【问题标题】:Not able to get mobile data enabled or disabled in nougat and marshmallow无法在牛轧糖和棉花糖中启用或禁用移动数据
【发布时间】:2017-06-05 07:13:08
【问题描述】:

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

<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 中打开/关闭移动数据时,我定义的接收器正在调用,但接收器没有在 marshmallow 和 nougat 中调用。

【问题讨论】:

  • 你得到任何 logcat 的错误是什么?
  • 我没有收到任何错误,当我在棉花糖和牛轧糖中手动启用或禁用移动数据时,接收器“CheckLockBroadCastReciever”不会自动撤销
  • 这里添加java代码
  • 公共类 CheckLockBroadCastReciever 扩展 BroadcastReceiver { Context mCONtext; @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Called, internet response called", Toast.LENGTH_SHORT).show(); } }
  • 抱歉,无法以正确的代码格式粘贴。没有调用 toast 消息。

标签: android android-6.0-marshmallow android-7.0-nougat


【解决方案1】:

使用permission:

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

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

【讨论】:

    【解决方案2】:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-22
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多