1

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

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

代码:

 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;
}

}
4

2 回答 2

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();
于 2013-09-03T11:42:33.590 回答
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();
        }
于 2015-06-07T05:39:58.253 回答