应用被杀死后,互联网广播接收器无法正常工作

时间:2019-07-01 08:21:01

标签: android broadcastreceiver

我收听与广播接收器的更改的Internet连接。在前台,一切都很好。但是当应用程序被杀死时,它不起作用。

清单:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

我的广播接收器(NetworkReceiver):

application.registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent{
            }, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

NetworkConstraint:

public class NetworkConstraint implements Constraint {

    @Override
    public boolean isConnected() {
        ConnectivityManager connectivityManager = (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }


    @Override
    public void applyToJobInfo(@NonNull JobInfo.Builder jobInfoBuilder) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        jobInfoBuilder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        }
    }

}

CellConstraint:

public class CellServiceConstraint implements Constraint {

    @Override
    public boolean isConnected() {
        return new TelephonyServiceState().isConnected(application);
    }

    @Override
    public void applyToJobInfo(@NonNull JobInfo.Builder jobInfoBuilder) {
    }
}

1 个答案:

答案 0 :(得分:1)

plz引用此链接https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html。 O不支持Connectivity_change操作。您可以尝试另一种方法。