【发布时间】: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