gpt4 book ai didi

android - Wifi Broadcastreceiver onreceive

转载 作者:行者123 更新时间:2023-11-30 03:33:22 25 4
gpt4 key购买 nike

我的 wifi 广播接收器有问题。它没有收到任何东西,永远不会调用 onReceive。这是我的代码:

public final class WifiChangeReceiver extends BroadcastReceiver {

boolean portableHotspot = true;

@Override
public void onReceive(final Context context, Intent intent) {

boolean alreadyPresent = false;

String action = intent.getAction();
if(action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
WifiManager w = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> l = w.getScanResults();

myApp myApp = (myApp)context.getApplicationContext();
List<ScanResult> previousScan = new ArrayList<ScanResult>();
previousScan = myApp.getPreviousScan();

System.out.print("previousScan " + previousScan + "\n");

for (ScanResult r : l) {
if(r.SSID.equals("\""+"TinyBox"+"\"")) {
if(previousScan!=null) {
for (ScanResult previousScanElement : previousScan) {
if(previousScanElement.SSID.contains("\""+"TinyBox"+"\"")) {
alreadyPresent = true;
break;
}
}
}
if (!alreadyPresent) {

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {

Methods.connectToNetwork(context);

}
}, 5000);

alreadyPresent = false;
}
}
}

myApp.setPreviousScan(l);


ConnectivityManager conMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo i = conMgr.getActiveNetworkInfo();

//Se non connesso a nessuna rete, allora tiro su TinyBox.
if (i == null) {

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

i = conMgr.getActiveNetworkInfo();
if (i == null) {
boolean portableHotspot = false;

final SQLiteDatabase db = DatabaseManager.getInstance(context).getWritableDatabase();

Cursor c = HotSpotActivation.getHotSpotState(db);

if (c.getCount() > 0) {
String activation = c.getString(c.getColumnIndex(HotSpotActivation.CHECK_STATE));

if (activation.equals("ON")) {
portableHotspot = true;
}
else {
portableHotspot = false;
}
}

if (portableHotspot) {
Methods.setWirelessHotSpot(context.getApplicationContext());
}
}
}
else {
}

}
else if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
int iTemp = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
checkState(iTemp);
}
else if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
DetailedState state=
((NetworkInfo)intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)).getDetailedState();
changeState(state, context);
}
}

private void changeState(DetailedState aState, Context context) {
if (aState == DetailedState.SCANNING) {
Log.d("wifiSupplicanState", "SCANNING");
}
else if (aState == DetailedState.CONNECTING) {
Log.d("wifiSupplicanState", "CONNECTING");
}
else if(aState == DetailedState.OBTAINING_IPADDR) {
Log.d("wifiSupplicanState", "OBTAINING_IPADDR");
}
else if (aState == DetailedState.CONNECTED) {
Log.d("wifiSupplicanState", "CONNECTED");
}
else if (aState == DetailedState.DISCONNECTING) {
Log.d("wifiSupplicanState", "DISCONNECTING");
}
else if (aState == DetailedState.DISCONNECTED) {

myApp myApp = (myApp)context.getApplicationContext();

portableHotspot = myApp.getPortableHotspot();

if (portableHotspot) {
Methods.setWirelessHotSpot(context.getApplicationContext());
}

}
else if (aState == DetailedState.FAILED) {
}
}

public void checkState(int aInt) {
if (aInt == WifiManager.WIFI_STATE_ENABLING) {
Log.d("WifiManager", "WIFI_STATE_ENABLING");
}
else if (aInt== WifiManager.WIFI_STATE_ENABLED) {
Log.d("WifiManager", "WIFI_STATE_ENABLED");
}
else if (aInt == WifiManager.WIFI_STATE_DISABLING) {
Log.d("WifiManager", "WIFI_STATE_DISABLING");
}
else if (aInt == WifiManager.WIFI_STATE_DISABLED) {
Log.d("WifiManager", "WIFI_STATE_DISABLED");
}
}
}

和 list :

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
<receiver android:name="sample.broadcastreceivers.WifiChangeReceiver">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"></action>
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE"></action>
<action android:name="android.net.wifi.STATE_CHANGE"></action>
<action android:name="android.net.wifi.SCAN_RESULTS"></action>
</intent-filter>
</receiver>

只有当我进入 wifi 设置菜单(列出网络的菜单)时,它才有效(在调用 Receive 时),没有按下任何东西,只是打开它。进入该菜单后,我立即开始从广播接收器接收消息,并且一切正常,没有任何原因。

问题出在哪里?这就像一个 Android 错误。我很确定以前有时使用相同的代码。谢谢

最佳答案

检查您的 AndroidManifest:您声明

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

?

关于android - Wifi Broadcastreceiver onreceive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17032376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com