0

我可以使用以下代码选择所有已安装的应用程序:

ActivityManager am = (ActivityManager)context.getSystemService(Activity.ACTIVITY_SERVICE);
    List Runningtasks = am.getRunningAppProcesses();

但我想要每个应用程序的启动计数?

4

2 回答 2

3

使用“SharedPreferences”获取计数非常简单。将下面的代码放在 OnCreate 方法下。

//声明必要的变量

  SharedPreferences sharedPreferences;
  int count;

  sharedPreferences = getPreferences(0);
  int count = sharedPreferences.getInt("numRun", 0);
  count++;
  sharedPreferences.edit().putInt("numRun", count).commit(); 
于 2013-09-12T06:22:45.590 回答
0

You can use polling the current application in the foreground by asking the current running task.

int currentUidInFront = 0;
    while(true)
    {
    ActivityManager actMan = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<RunningTaskInfo> runningApps = actMan.getRunningTasks(1024);
    RunningTaskInfo runningTask = runningApps.get(0);
    int uidInFront = getApplicationContext().getPackageManager().getApplicationInfo(runningTask.topActivity.getPackageName(),1).uid;

    //chack if the current application is changed
    if(currentUidInFront != uidInFront)
    {
       currentUidInFront = uidInFront;
       Log.i("Changed running application",runningTask.topActivity.flattenToShortString());
    }  
  sleep(1000);
}

hope this helped. enjoy

于 2013-09-12T07:49:40.187 回答