【问题标题】:Netowork Broadcast Receiver not working for Android N网络广播接收器不适用于 Android N
【发布时间】:2017-07-10 03:39:00
【问题描述】:

我有一个用于连接更改的广播接收器,它适用于在 android N 下运行的所有设备,但它不能在 android N 上运行。无论它们是否在 N 中更改某些内容,我都找不到与此相关的任何内容。有人知道为什么会这样吗?

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.movies">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
    android:name=".MovieSingleton"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".NetworkBroadcastReceiver"
        android:exported="false"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
</application>

这是我的广播接收器

public class NetworkBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = NetworkBroadcastReceiver.class.getSimpleName();

public NetworkBroadcastReceiver() {

}

@Override
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "onReceive: network change");
    if(isOnline(context)){
        Log.d(TAG, "onReceive: connected");
    } else {
        Log.d(TAG, "onReceive: not connected");
    }
}


public boolean isOnline(Context mContext) {
    ConnectivityManager connMgr = (ConnectivityManager)
            mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnected());
}

}

【问题讨论】:

标签: android broadcastreceiver android-7.0-nougat


【解决方案1】:

由于许多应用注册以接收此广播,因此单个网络切换可以使它们全部唤醒并立即处理广播。

在 Android Nougat 中,您必须使用 Context.registerReceiver() 注册 BroadcastReceiver。

此更改是 Android 7.0 后台优化和性能改进的一部分。

【讨论】:

    猜你喜欢
    • 2022-06-11
    • 2013-09-17
    • 2018-03-25
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2022-05-14
    • 1970-01-01
    • 2019-03-02
    相关资源
    最近更新 更多