简体   繁体   中英

Managing background running processes

I am trying to freeze background running processes when mobile screen is off and when screen get on processes should restart automatically in lower android versions. But i am not able to find any code to achieve this functionality.So if anyone knows about this then please help.

Try this :)

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            //Stop your service
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // Start your service
        }
    }
}, intentFilter);

You can stop your background service in onStop() method of activity life cycle by using stopService(intent); and again start in onResume() method of activity life cycle by using startService(intent);

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