我使用广播接收器检测wifi连接。它可成功用于Android Lolipop。但是当我尝试使用Pie(android 9)时,它不起作用。我将广播内容注册在AndroidManifest.xml中,如下所示。
<receiver android:name=".ExampleBroadcast">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
示例广播是我的课程,它扩展了广播接收器。它的工作原理如下。
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.widget.Toast;
public class ExampleBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//required task
}
}
上面的代码适用于android 5,不支持android Pie。我想知道什么是正确的注册标准,什么是适用于android 7的android.net.conn.CONNECTIVITY_CHANGE的兼容操作。
最佳答案
我知道在Android的最新版本中,许多与能耗和进程间通信有关的策略已更改;广播接收器也已更改。
如果您的解决方案基于更统一的答案,例如"How to Detect CONNECTIVITY_CHANGE",而不是基于我的新书面答案,那么对您而言,我会发现更有用。
如果链接的答案对您没有用,我建议您在Google上使用诸如“ Android Pie连接更改广播接收器”之类的关键字进行快速搜索,以找到适合您特定问题的最佳答案。