简体   繁体   中英

How to start background service when user start using android device?

I have implemented an Android service ( START_STICKY ) which starts on device boot and runs in background. Functionality of this service is to interact with SD card. As it is running continuously, started with sticky it consumes battery. To solve heavy battery consumption I wanted to start this service when user is using device.

Ideally start/stop service based on the ACTION_SCREEN_ON & ACTION_SCREEN_OFF intents.

When I have tested this found that I can't register for ACTION_SCREEN_OFF & ACTION_SCREEN_ON in the Manifest, so I have created a Broadcast Receiver in my service to capture the ACTION_SCREEN_OFF & ACTION_SCREEN_ON .

But, since I cannot register for the intents in the manifest, when I stop my service on ACTION_SCREEN_OFF . How can I possibly start it when the screen comes back on?

Note : As I have already mentioned SCREEN_ON + SCREEN_OFF cannot be registered in manifest file. It is registered like

// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC 

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
broadcastReceiver = new TestReceiver();
registerReceiver(broadcastReceiver, filter); 

So when service is not running then this intent will not fire.

you can use BroadCastReciever to call your service based on the type of broadcast

public class MyReceiver extends BroadcastReceiver {

     public static boolean wasScreenOn = true;

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

            // do whatever you need to do here

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
              // and do whatever you need to do here

        }
       else if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
        {
          // and do whatever you need to do here
       }

    }

Have a Broadcast Receiver for ACTION_SCREEN_ON, ACTION_SCREEN_OFF and ACTION_USER_PRESENT.

onResume of the activity will be called when ACTION_SCREEN_ON is fired. Create a handler and wait for ACTION_USER_PRESENT. When it is fired, implement what you want for your activity.

Register receiver as below:

private void registBroadcastReceiver() {
    final IntentFilter theFilter = new IntentFilter();
    /** System Defined Broadcast */
    theFilter.addAction(Intent.ACTION_SCREEN_ON);
    theFilter.addAction(Intent.ACTION_SCREEN_OFF);

    mPowerKeyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String strAction = intent.getAction();

            if (strAction.equals(Intent.ACTION_SCREEN_OFF) || strAction.equals(Intent.ACTION_SCREEN_ON)) {
                // > Your playground~!
            }
        }
    };

    getApplicationContext().registerReceiver(mPowerKeyReceiver, theFilter);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM