Android 监控最近的应用程序

Android 监控最近的应用程序,android,android-intent,Android,Android Intent,我正在编写一个应用程序,用于监控我在Android设备中使用最多的应用程序 为此,我正在使用: final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); final List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(20, ActivityMa

我正在编写一个应用程序,用于监控我在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());

        }
final ActivityManager ActivityManager=(ActivityManager)getSystemService(Context.ACTIVITY_服务);
最终列表recentTasks=activityManager.getRecentTasks(20,activityManager.RECENT_,不包括_);
对于(int i=0;i
问题是is只给我最近的应用,而不是每个应用启动了多少次

若要按最近的应用进行检查,我会检查该应用是否比上次检查时更新,这样我就知道它已运行

如果每次通话间隔约为3小时,则可能会有一个应用程序被多次调用,然后我只将其计算为一个

在给定时间间隔的情况下,是否有方法接收应用程序启动的次数


我知道这是一个特定的问题,但若有人遇到类似的问题,那个将是很有帮助的。(也许是有意的?)

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

试试这个,但是有一些inter-api

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