Android : How to use registerReceiver in BroadcastReceiver -


i want perform task when action_boot_completed intent, i'm using following code check if device plugged in method undefined type bootreceiver @ registerreceiver. can find solution? here i'm doing.

public class bootreceiver extends broadcastreceiver {  @override public void onreceive(final context context, intent intent) {      if (intent.getaction().equalsignorecase(intent.action_boot_completed)) {      if(ischarging())     dosomething();  //something perform after boot if plugged in          }            public boolean ischarging() {     intentfilter filter = new intentfilter(intent.action_battery_changed);     intent batterystatus = registerreceiver(null, filter); //getting error here      boolean strstate;      int chargestate = batterystatus.getintextra(batterymanager.extra_status, -1);      switch (chargestate) {         case batterymanager.battery_status_charging:         case batterymanager.battery_status_full:             strstate = true;             break;         default:             strstate = false;     }     return strstate;      }  } 

call registerreceiver(null, new intentfilter(intent.action_battery_changed)). return intent has extras defined on batterymanager let know if plugged in or not.

this works because intent.action_battery_changed sticky broadcast.

answer picked check if device plugged in commonsware

update: please call context.registerreceiver(null, new intentfilter(intent.action_battery_changed)); method defined in context


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -