2012-02-26 93 views
1

我正在编写一个应用程序,它监视我在Android设备中使用的最多的应用程序。监控最近的应用程序

要做到这一点,我使用:

final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
    final List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(20, ActivityManager.RECENT_WITH_EXCLUDED); 

    for (int i = 0; i < recentTasks.size(); i++) { 

     Intent baseIntent = recentTasks.get(i).baseIntent; 
     if(baseIntent != null) { 

      Log.d("Text", "Lior: Application executed: " + i + ": baseIntent: " + baseIntent.getComponent().getPackageName() + baseIntent.getComponent().getClassName()); 

     } 

的问题,这是只给了我最近的应用程序,而不是每个应用程序了多少次启动。

要检查最近的应用程序,我检查应用程序现在是否比上次检查时更新 - 这种方式我知道它已运行。

由于每次通话时间约为3小时,可能会有一个应用程序被多次调用,然后我只会将其计为一次。

有没有办法接收一个应用程序在给定时间差的情况下启动了多少次?

我知道这是一个具体的问题,但如果有人遇到类似的情况,这将是有益的。 (也许是有意图的东西?)

回答

0

我还没有玩过小部件,但也许你可以将你的应用程序设置为一个。然后每次打开应用程序添加到绑定到该应用程序的int。

0

试试这个,但有一些inter apis。

private void setActivityController() { 
    IActivityManager am = ActivityManagerNative.getDefault(); 
    try { 
     am.setActivityController(new ActivityController()); 
    } catch (RemoteException e) { 
     e.printStackTrace(); 
    } 
} 




public class ActivityController extends IActivityController.Stub { 

private static final String TAG = ActivityController.class.getSimpleName(); 

@Override 
public boolean activityResuming(String pkg) throws RemoteException { 
    Log.e(TAG, "activityResuming -- "+pkg); 
    return true; 
} 

@Override 
public boolean activityStarting(Intent intent, String pkg) 
     throws RemoteException { 
    Log.e(TAG, "activityStarting -- "+pkg+" intent="+intent); 
    return true; 
} 

@Override 
public boolean appCrashed(String processName, int pid, String shortMsg, String longMsg, 
     long timeMillis, String stackTrace) throws RemoteException { 
    Log.e(TAG, "appCrashed -- "+processName); 
    return true; 
} 

@Override 
public int appEarlyNotResponding(String processName, int pid, String annotation) 
     throws RemoteException { 
    Log.e(TAG, "appEarlyNotResponding -- "+processName); 
    return 0; 
} 

@Override 
public int appNotResponding(String processName, int pid, String processStats) 
     throws RemoteException { 
    Log.e(TAG, "processName -- "+processName); 
    return 0; 
} 

}