Ajay Kurmi
Ajay Kurmi

Reputation: 183

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

I have added below permission and defined broadcast receiver

<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>`

My defined receiver is calling when I am doing mobile data on/off in jellybeans os, KitKat os and lollipop os but the receiver is not invoking in marshmallow and nougat.

Upvotes: 0

Views: 445

Answers (2)

Shripad Jadhav
Shripad Jadhav

Reputation: 316

The network changes listener implementation for android N and high

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        registerReceiver(mNetworkReceiver, 
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
 }

Please refer https://developer.android.com/about/versions/nougat/android-7.0-changes.html

Upvotes: 0

W4R10CK
W4R10CK

Reputation: 5550

Use permission:

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

Also, try to register your reciever inside onCreate and don't forget to unregister and register inside onPause and onResume respectly.

Upvotes: 1

Related Questions