牛轧糖中的互联网连接

Internet connection in Nougat

我想在我的应用程序中检查互联网连接,但此代码不适用于 android 牛轧糖以检查互联网连接 此代码适用于棉花糖和奇巧但不适用于牛轧糖 请帮助我

广播代码:

public class NetworkChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    final ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo=connMgr.getActiveNetworkInfo();
    if(networkInfo!=null && networkInfo.isConnectedOrConnecting()){
        Toast.makeText(context, "connected", Toast.LENGTH_SHORT).show();
    }


}
}

清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="clicksite.org.testinternetconnection">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_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:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <receiver android:name=".NetworkChangeReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>


</application>

</manifest>

显然,向清单中添加广播接收器已被弃用。

Android 文档给出了这样的解释:

Apps targeting Android 7.0 (API level 24) and higher do not receive CONNECTIVITY_ACTION broadcasts if they declare the broadcast receiver in their manifest. Apps will still receive CONNECTIVITY_ACTION broadcasts if they register their BroadcastReceiver with Context.registerReceiver() and that context is still valid.

看看是否能解决您的问题。

IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
this.registerReceiver(new NetworkConnectionReceiver(), intentFilter);

这可能也很有趣: https://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver,%20android.content.IntentFilter)

我遇到了同样的问题。我正在考虑创建一个在后台运行并耐心等待广播的服务。

我实际上用 Task Scheduler us android evernote here job library then

public static void scheduleAdvancedJob() {
    PersistableBundleCompat extras = new PersistableBundleCompat();
    extras.putString("key", "smj");

    int jobId = new JobRequest.Builder(MainConstant.TAG)
            .setExecutionWindow(3_000L, 30_000L)
            .setBackoffCriteria(5_000L, JobRequest.BackoffPolicy.EXPONENTIAL)
            .setRequiresCharging(false)
            .setRequiresDeviceIdle(false)
            .setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
            .setExtras(extras)
            .setRequirementsEnforced(true)
            .setUpdateCurrent(true)
            .build()
            .schedule();
}