我正在尝试启用和禁用我的wifi广播接收器,但什么也没发生。我希望有人可以帮助我知道问题在哪里。提前谢谢你。
这是我正在使用的代码:
编辑我添加onResume onPause活动,并且应用程序开始扫描,就像启用和禁用功能不存在一样。 这是我的主要活动:
public class MainActivity extends Activity {
WifiManager wifi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
}
/**Mthods added in the edit **/
public void onPause() {
super.onPause();
this.unregisterReceiver(broad);
}
/**
* Broadcast receiver to update
*/
public void onResume() {
super.onResume();
//Note: Not using RSSI_CHANGED_ACTION because it never calls me back.
super.registerReceiver(broad, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
IntentFilter rssiFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
this.registerReceiver(broad, rssiFilter);
WifiManager wifiMan=(WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiMan.startScan();
}
/**End Mthods added in the edit **/
/**
* This method enables the Broadcast receiver registered in the AndroidManifest file.
* @param view
*/
public void enableBroadcastReceiver(View view){
ComponentName receiver = new ComponentName(this, BroadcsatReceiverMnager.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT).show();
}
/**
* This method disables the Broadcast receiver registered in the AndroidManifest file.
* @param view
*/
public void disableBroadcastReceiver(View view){
ComponentName receiver = new ComponentName(this, BroadcsatReceiverMnager.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
}
}
这是我的BroadcsatReceiverMnager类
public class BroadcsatReceiverMnager extends BroadcastReceiver {
int newRssi;
int rssi1 ;
int rssi2;
int rssi3 ;
int res = rssi1+rssi2+rssi3;
int rssiOp1=0 ;
int rssiOp2 =0 ;
int rssiOp3=0 ;
long distanceTemp;
WifiManager wifi;
protected void onCreate(Context context) {
wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
}
@Override
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0,"MyTag BroadcsatReceiverMnager "+ "onReceive", Toast.LENGTH_LONG).show();
wifi = (WifiManager)arg0.getSystemService(Context.WIFI_SERVICE);
if(arg1.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION )){
List<ScanResult> results = wifi.getScanResults();
Toast.makeText(arg0, "results"+results, Toast.LENGTH_SHORT).show();
rssiOp1 = results.get(0).level;
rssiOp2 = results.get(1).level;
rssiOp3 = results.get(2).level;
}
WifiManager wifiMan=(WifiManager)arg0.getSystemService(Context.WIFI_SERVICE);
wifiMan.startScan();
int newRssi = wifiMan.getConnectionInfo().getRssi();
Toast.makeText(arg0, "BroadcsatReceiverMnager"+newRssi, Toast.LENGTH_SHORT).show();
}
}
这是manifest.xml的一部分 EDITED:
<!-- Broadcast receiver -->
<receiver android:name="com.example.downloadservicetest1.BroadcsatReceiverMnager" >
<intent-filter>
<action android:name="android.net.wifi.supplicant.STATE_CHANGE" />
</intent-filter>
</receiver>