简体   繁体   English

管理后台运行流程

[英]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. 我正在尝试在移动屏幕关闭时冻结后台运行的进程,并且当屏幕启动时,进程应在较低的android版本中自动重新启动。 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); 您可以使用stopService(intent);在活动生命周期的onStop()方法中停止后台服务stopService(intent); and again start in onResume() method of activity life cycle by using startService(intent); 然后再次使用startService(intent);从活动生命周期的onResume()方法开始startService(intent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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