问题
I have a BroadcastReceiver that detects internet availablity changes. The code is working the app says online when internet is available and when I turn of WIFI or 4G it says offline. I have added this line of code to prevent the application from closing when back button is pressed:
@Override
public void onBackPressed() {
this.moveTaskToBack(true);
}
When I press back button the application is put on background and the BroadcastReceiver says always offline even if internet is available. Why is it not working when app is put in background?
Thanks.
Code is below:
public class MainActivity extends AppCompatActivity {
private NetworkReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
receiver = new NetworkReceiver();
registerReceiver(receiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
@Override
public void onBackPressed() {
this.moveTaskToBack(true);
}
}
public class NetworkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(isConnected(context)) {
Toast.makeText(context, "Online", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Offline", Toast.LENGTH_LONG).show();
}
}
public boolean isConnected(Context context) {
boolean status = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected) {
status = true;
}
return status;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.e.myapplication">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
回答1:
You have a dynamically registered BroadcastReceiver
, when you move your app to background, this receiver is likely to be recycled by the Android system since this receiver is bound to the life cycle of your activity and the activity could very likely be recycled by Android since the activity is not supposed to run any background tasks. I suppose that's why even though sometimes you can still get broadcasts with this code, the stuff you are using inside returns false results because some of the components you are using are recycled by android already which results in the invalid result.
If you want to have a broadcast receiver that works in the background, register the broadcast receiver in the AndroidManifest
with the corresponding intent filters and your implementation of a receiver service that will be fired once the broadcast is received.
来源:https://stackoverflow.com/questions/55342572/broadcastreceiver-to-detect-internet-on-background