简体   繁体   English

如何暂停前台服务

[英]How to pause a foreground service

I have a service which is a foreground service which runs even when the app is kill. 我有一个服务,它是一个前台服务,即使在应用程序被杀死时也可以运行。 I just wanted to know is there any way I can pause this service when user move to some other activity and resume back when user come back to previous activity. 我只想知道有什么方法可以在用户移至其他活动时暂停此服务,并在用户返回上一个活动时恢复。

If you want to kill background services use this . 如果您想终止后台服务,请使用this。

List<ApplicationInfo> packages;
            PackageManager pm;
            pm = getPackageManager();
            //get a list of installed apps.
            packages = pm.getInstalledApplications(0);
            ActivityManager mActivityManager = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
            String myPackage = getApplicationContext().getPackageName();
            for (ApplicationInfo packageInfo : packages) {
                if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
                if(packageInfo.packageName.equals(myPackage)) continue;
                mActivityManager.killBackgroundProcesses(packageInfo.packageName);
            }

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

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