我创建了一个用于Wi-Fi的brodcastReceiver,每件事情都运行良好,除了当我在应用程序午餐时,广播接收器开始直接扫描,我无法阻止它,即使我出去也一直在运行从申请。我试图从主要活动中控制广播接收器,但似乎有一个问题。 有人可以查看我的代码,如果有可能,请告诉我可能是什么问题。
提前谢谢。
PS :点击按钮时,祝酒词正在运行,但对brodcast接收器没有任何影响。
我在brodcastreceiver上午餐,是否会对这个问题产生影响?
**EDIT**
这是我的MainActivity
public class MainActivity extends Activity {
int newRssi;
List<String> listDebitDistance = new ArrayList<String>();
WifiManager wifi;
private final BroadcsatReceiverMnager broad = new BroadcsatReceiverMnager();
ArrayList<int[]> listCursorReçue = new ArrayList<int[]>();
IntentFilter rssiFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method enables the Broadcast receiver registered in the AndroidManifest file.
* @param view
*/
public void enableBroadcastReceiver(View view){
switch (view.getId()){
case R.id.button1 :
super.registerReceiver(broad, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
this.registerReceiver(broad, rssiFilter);
WifiManager wifiMan=(WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiMan.startScan();
Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT).show();
break;
}
}
/**
* This method disables the Broadcast receiver registered in the AndroidManifest file.
* @param view
*/
public void disableBroadcastReceiver(View view){
switch (view.getId()){
case R.id.button2 :
//this.unregisterReceiver(broad); //this gives the exception
unregisterReceiver(broad); //this methode gives the exception too
Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
break;
}
}
public void onPause() {
super.onPause();
this.unregisterReceiver(broad);
//S'il y a un appel l'app crash !!! a voir !!!!
}
/**
* Broadcast receiver to update
*/
public void onResume() {
super.onResume();
// BroadcsatReceiverMnager broad = new BroadcsatReceiverMnager();
//Note: Not using RSSI_CHANGED_ACTION because it never calls me back.
WifiManager wifiMan=(WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiMan.startScan();
}
}
这是我的BroadcsatReceiverMnager类
public class BroadcsatReceiverMnager extends BroadcastReceiver {
int newRssi;
int rssi1 ;
int rssi2;
int rssi3 ;
int rssiOp1=0 ;
int rssiOp2 =0 ;
int rssiOp3=0 ;
WifiManager wifi;
@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();
}
}
答案 0 :(得分:1)
如果您想控制接收者的注册和取消注册,请不要在您的清单中定义它。正如我在answer中所说的那样:
如果您在清单中注册接收器,接收器的处理程序将在每次通信事件发生时启动。例如:每当您有连接向您显示通知时,Facebook的信使就会被启动...或者当您连接建议更新时,其他应用程序被启动...换句话说,接收器始终是注册的。
在您的情况下,在您的活动中定义接收者,注册它并在同一活动中取消注册。
/**
* the intent of communication with the Brodcast receiver
*/
IntentFilter intentFilter = new IntentFilter();
/**
* the BroadcastReceiver
*/
BroadcastReceiver yourBroadcastReceiver = new BroadcsatReceiverMnager ();
在onCreat()
:
// set the action
intentFilter.addAction("SCAN_RESULTS_AVAILABLE_ACTION");
注册:
registerReceiver(yourBroadcastReceiver , intentFilter);
取消注册:
unregisterReceiver(yourBroadcastReceiver);
所以,在你的听众中:
/**
* This method enables the Broadcast receiver registered in the AndroidManifest file.
* @param view
*/
public void enableBroadcastReceiver(View view){
switch (view.getId()){
case R.id.button1 :
registerReceiver(yourBroadcastReceiver , intentFilter);
Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT).show();
break;
}
}
/**
* This method disables the Broadcast receiver registered in the AndroidManifest file.
* @param view
*/
public void disableBroadcastReceiver(View view){
switch (view.getId()){
case R.id.button2 :
unregisterReceiver(yourBroadcastReceiver);
Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
break;
}
}
评论中你最后一个问题的答案:
1 /你有扫描要做,以获得一些信息。
2 /您希望仅在您处于主要活动时,通过操作(通过意图过滤器)进行扫描。
===&GT;您在此活动中使用接收器: - 在onResume()中注册它并在onPause()中注销它 - 在接收器的onReceive()中进行扫描(以及你要对它将返回的数据做什么)。
3 /你想要管理什么时候激活接收器,什么时候需要它:接收器不仅取决于活动的生命周期: ===&GT;然后你添加一个按钮来激活或禁用它(注册/取消注册)。