Search code examples
androidbroadcastreceiver

Broadcast receiver is not working when app on foreground or active


I am building app regarding battery indicator and i am using code from this post.

Getting battery status even when the application is closed

it is working fine when app is closed, but when an app is active or on foreground it did not work or did not send any broadcast.

This is main activity from i start service

public class Main extends Activity {

private MyService service;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (service == null) {

        Intent i = new Intent(this, MyService.class);
        startService(i);
    }

    finish();
}

}

Following is the service code.

public class MyService extends Service{

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("MyService", "onStartCommand");
    // do not receive all available system information (it is a filter!)
    final IntentFilter battChangeFilter = new IntentFilter(
            Intent.ACTION_BATTERY_CHANGED);
    // register our receiver
    this.registerReceiver(this.batteryChangeReceiver, battChangeFilter);
    return super.onStartCommand(intent, flags, startId);
}
private final BroadcastReceiver batteryChangeReceiver = new BroadcastReceiver() {

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

private void checkBatteryLevel(Intent batteryChangeIntent) {
    // some calculations
    final int currLevel = batteryChangeIntent.getIntExtra(
            BatteryManager.EXTRA_LEVEL, -1);
    final int maxLevel = batteryChangeIntent.getIntExtra(
            BatteryManager.EXTRA_SCALE, -1);
    final int percentage = (int) Math.round((currLevel * 100.0) / maxLevel);

    if(percentage==100)
    {
        Intent intent = new Intent(getBaseContext(), Last.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(intent);

    }
    // do not forget to unregister
    unregisterReceiver(batteryChangeReceiver);
} }

And when following activity start i did not receive any broadcast.

public class Last extends Activity {
Button btnCancel;
Uri notification;
Ringtone r;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_last);

    notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
    r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();


     btnCancel = (Button) findViewById(R.id.stopsound);

     btnCancel.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

             r.stop();
        }
    });

}  }

Solution

  • As I understood, when you start application in first time, you see nothing, just service is started and a broadcast receiver is registered. When battery level will be changed, the method checkBatteryLevel() is calling and the broadcast receiver will be unregistered. As result you have never received a new changing of battery level.