2012-02-26 94 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; 
} 

}