无法在牛轧糖和棉花糖中启用或禁用移动数据

Not able to get mobile data enabled or disabled in nougat and marshmallow

我添加了以下权限并定义了广播接收器

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".CheckLockBroadCastReciever" android:enabled="true" android:exported="false">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <!--<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />-->
        </intent-filter>
    </receiver>
</application>`

当我在 jellybeans os、KitKat os 和 lollipop os 中执行移动数据 on/off 时,我定义的接收器正在调用,但接收器未在 marshmallow 中调用和牛轧糖。

使用permission:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

此外,尝试在 onCreate 中注册您的接收器,不要忘记分别在 onPauseonResume 中注销和注册。

网络更改 android N 和 high

的侦听器实现
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        registerReceiver(mNetworkReceiver, 
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
 }

请参考https://developer.android.com/about/versions/nougat/android-7.0-changes.html