开发者

Android: Creating a persistent event listener

开发者 https://www.devze.com 2023-04-10 06:56 出处:网络
I am trying to figure out how to implement an event listener (unsure if this is the proper term.) I want the service, once my app is launched, to listen for the phones power status. I am uncertain to

I am trying to figure out how to implement an event listener (unsure if this is the proper term.) I want the service, once my app is launched, to listen for the phones power status. I am uncertain to as how android handles this situation so don't really know what to search for. I've been working with this code that uses a broadcast receiver:

 BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            unregisterReceiver(this);
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
            if (plugged == BatteryManager.BATTERY_PLUGGED_AC) {
                // on AC power
                 Toast.makeText(getApplicationContext(), "AC POWER", Toast.LENGTH_LONG).show();
            } else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
                // on USB power
             Toast.makeText(getApplicationContext(), "USB POWER", Toast.LENGTH_LONG).show();
                 startActivity(alarmClockIntent);
            } else if (plugged == 0) {
                Toast.makeText(getApplicationContext(), "On Battery", Toast.LENGTH_LONG).show();
            } else {
                // intent didnt include extra info
            }
        }
    };
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(receiver, filter);

The code works fine. When I open my app it will toast what the current status of the phone power is.

Here is what I am trying to do:

  • When the user launches the app, it is effectively turning on the service
  • The user can go about using the phone, but once it is plugged in, my service will catch that and use the code above

How do I ada开发者_如何学Gopt this code to achieve the objectives above?


You could keep the listener on for the battery status by removing the line

unregisterReceiver(this);

This way, the app will continue to listen to power status change in the background even though that the app is not running in the foreground. Note that at some point, you might still want to unregister your receiver. You probably want to allow the user to control that via settings.

One other note, your code contains starting activity in the receiver in below code:


else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
    // on USB power
    Toast.makeText(getApplicationContext(), "USB POWER", Toast.LENGTH_LONG).show();
    startActivity(alarmClockIntent);
} 

If your activity is in the background then it can't start another activity. See this SO Question - how to start activity when the main activity is running in background?, the accepted answer has suggestion on how to handle situation that requires starting activity from the background

0

精彩评论

暂无评论...
验证码 换一张
取 消