سؤال

I'm using broadcast receiver to scan the RSSI change ,well in every change i had to do some computings. But the broadcast receiver is scanning too fast ,so it can not do the computing task. I know that ,when it detect a change in the RSSI it start a new loop. How can i stop the execution of the broadcast for a little periode to do some calculation, and then resume. this is my BroadcastReceiver :

private BroadcastReceiver myRssiChangeReceiver  = new BroadcastReceiver(){

@Override
public void onReceive(Context arg0, Intent arg1) {
    Toast.makeText(Position.this,"MyTag distance "+ "onReceive", Toast.LENGTH_LONG).show();
   (WifiManager)Position.this.getSystemService(Context.WIFI_SERVICE);
    wifiMan.startScan();
  newRssi = wifiMan.getConnectionInfo().getRssi();

  Toast.makeText(Position.this, ""+newRssi, Toast.LENGTH_SHORT).show();
  int size = listCursorReçue.size();
  for (int j=0;j<size;j++){
     int[] ligne = listCursorReçue.get(j);
     Toast.makeText(Position.this,"MyTag BroadcastReceiver "+ ligne[2] + ligne [3] +"loguer ligne"+ligne.length, Toast.LENGTH_LONG).show();
    distanceTemp = (long) Math.sqrt((rssi1-rssiOp1)*(rssi1-rssiOp1)+(rssi2-rssiOp2)*(rssi2-rssiOp2)+(rssi3-rssiOp3)*(rssi3-rssiOp3));
}




}};

Thank you in advance.

هل كانت مفيدة؟

المحلول

If your BroadcastReceiver is registered on main thread(UI thread), then it will be killed after 10 seconds. You can read about it here.

If you wan't to do something which takes much time you have two options:

  1. Use another thread for receiver
  2. Start a Service from your onReceive and perform long-running task within a Service.

I would go with second option. That'll give you more clean and flexible architecture.

UPD

More on Services here.

There's a range of options with Services. So I sugges you to learn more about Service lifecycle and then choose what fits best for your problem.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top