简体   繁体   中英

android broadcast receiver to detect app in background change

Is there anyway to capture when other application goes into background? Any broadcast receiver we can register to receive that event?

出于明显的隐私原因,没有关于前往或离开前台的活动的广播Intent

You can write a backgroung service that periodically monitors the running services and if a new service comes up raises a flag. Use this to get the list of running services:

    public List<String> getRunningServices(Context context){
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager
                .getRunningServices(Integer.MAX_VALUE);
        List<String> rServices = new ArrayList<String>();
        for (int i = 0; i < serviceList.size(); i++) {
            RunningServiceInfo serviceInfo = serviceList.get(i);
            ComponentName serviceName = serviceInfo.service;            
            rServices.add(serviceName.getClassName());      
        }
        return rServices;
   }

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