gpt4 book ai didi

Android BroadcastReceiver : Unable to start and stop BroadcastReceiver, 手动,自动启动

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

我为 Wi-Fi 创建了一个 brodcastReceiver,一切正常,除了,当我午餐应用程序时,broadcastreceiver 开始直接扫描,我无法停止它,即使我离开应用。我试图从主要 Activity 中控制广播接收器,但似乎存在问题。谁能看看我的代码,如果可能的话,告诉我可能是什么问题。

提前谢谢你。

PS :当点击按钮时,toasts起作用,但对广播接收者没有影响。

我正在 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();
}


}

最佳答案

如果你想控制你的接收者的注册和注销,根本不要在你的 list 中定义它。正如我在这个 answer 中所说的:

如果您在 list 中注册接收器,接收器的处理程序将在每次相应事件到来时启动。示例:每当您建立连接以向您显示通知时,facebook 的 Messenger 就会启动……或者当您连接以建议更新时,其他应用程序就会启动……换句话说,接收者始终处于注册状态。

在你的例子中,在你的 Activity 中定义接收者,在同一个 Activity 中注册和注销它。

/**
* 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/你只想在主要 Activity 中通过操作(通过 Intent 过滤器)来午餐扫描。

===> 您在此 Activity 中使用接收器:- 在 onResume() 中注册并在 onPause() 中取消注册- 在接收器的 onReceive() 中进行扫描(以及您想对它将返回的数据执行的操作)。

3/您想管理何时激活接收器,何时禁用它:接收器不仅取决于 Activity 的生命周期:===> 然后添加一个按钮来激活或禁用它(注册/注销)。

关于Android BroadcastReceiver : Unable to start and stop BroadcastReceiver, 手动,自动启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23263163/

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