-3
私はFirebaseを使用しています。私のアプリのいくつかの機能は、オフラインで使用できない(または、将来、オフラインモードになる可能性があります)。だから、どのように私は接続を検出することができますが失われた、またはwifi/otherNetworkは活動を実行中にオフです。私はこのdocに従っていますが、起動アプリは実行中のアプリでは動作しません。だから私の問題の解決方法はありますか?接続が失われたことをどのように検出できますか?
<receiver android:name=".utils.NetwrokConnection.NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
使用あなたがしたい活動で、この方法:
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Intent networkStateIntent = new Intent(Constants.NETWORK_AVAILABLE_ACTION);
networkStateIntent.putExtra(Constants.IS_NETWORK_AVAILABLE, isConnectedToInternet(context));
LocalBroadcastManager.getInstance(context).sendBroadcast(networkStateIntent);
}
public boolean isConnectedToInternet(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in airplane mode it will be null
if (netInfo != null && netInfo.isConnected()) {
return true;
} else {
return false;
}
}
は、このようなマニフェストファイル内のレシーバを登録します。