監控Activity的啟動等狀態--- 原始碼級

夢書發表於2014-11-05

1.程式碼

參見:http://stackoverflow.com/questions/9452549/monitoring-the-recent-apps

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

2.需要申明許可權:

 

    <uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER"/>

 

相關文章