从服务中查找在前台运行的应用程序

时间:2013-09-03 11:27:37

标签: android

我想从我的Service类中找到在前台运行的应用程序。我曾使用RunningAppProcessInfo.IMPORTANCE_FOREGROUND来检测它。但是出现以下问题:

  1. 它在前台提供了许多应用程序状态。
  2. 如果我打开任何应用程序,则它不显示其包名称。
  3. 代码:

     public class TapCountService extends Service{
    
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }
    
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        List<RunningAppProcessInfo> procInfos=getRunningAps();
        int loop=0;
        for (RunningAppProcessInfo runningAppProcessInfo : procInfos) {
            Toast.makeText(TapCountService.this, procInfos.get(loop).processName, Toast.LENGTH_SHORT).show();
            System.out.println("Process Name=====>"+procInfos.get(loop).processName);
            if(procInfos.get(loop).importance== RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
                Toast.makeText(TapCountService.this, procInfos.get(loop).processName, Toast.LENGTH_SHORT).show();
            } 
            ++loop;
        }
        return super.onStartCommand(intent, flags, startId);
    }
    
    /**
     * This method returns list of running application.
     * @return List<RunningAppProcessInfo>: containing application informations.
     */
    private List<RunningAppProcessInfo> getRunningAps(){
        ActivityManager actvityManager = (ActivityManager)this.getSystemService( ACTIVITY_SERVICE );
        List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
        return procInfos;
    }
    
    }
    

2 个答案:

答案 0 :(得分:5)

以下是获取当前预备应用程序的步骤。

1 getForegroundApp - returns the foreground application.
2 getActivityForApp - returns the activity of the found app.
3 isStillActive - determines if a earlier found app is still the active app.
4 isRunningService - a helper function for getForegroundApp

以下是代码

   // Add permission 
   <uses-permission android:name="android.permission.GET_TASKS"/>

    ActivityManager am = (ActivityManager)   AppService.this.getSystemService(ACTIVITY_SERVICE);
  // The first in the list of RunningTasks is always the foreground task.
   RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
   String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();
   PackageManager pm = AppService.this.getPackageManager();
   PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0);
   String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString();

答案 1 :(得分:2)

在“服务”中使用此代码来获取前台应用并执行自己的任务。

 try {
            ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();

            //   final String packageName = context.getPackageName();
            for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
                //   if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals()) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    Log.i("Foreground App", appProcess.processName);
                    String B2MAppsPKGName = appProcess.processName;

                    ////////Select Package name From SQLite DataBase//////////

                    OurStoreAppsPackages = new ArrayList<String>();
                    OurStoreAppsPackages = databaseHelper.getOurAllAppsPackage();

                    if (OurStoreAppsPackages.contains(B2MAppsPKGName)) {
                        //  if (storedPackageName == true) {
                        Log.i("DatabaseForeground App", B2MAppsPKGName);

                        ////// Go to GameStartPage page ./////////
                        Intent ii = new Intent(this, GameStartPage.class);
                        ii.putExtra("pkgName", B2MAppsPKGName);
                        ii.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(ii);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }