在源码中监听activity启动
3469 点击·0 回帖
![]() | ![]() | |
![]() | 上周做一个小的功能,修改statusbar,在launcher界面和其它应用界面显示不同的背景色和icon,最初想的就是接受系统activity启动的广播,进行判断,研究了一段时间,发现接受不到广播,最后在网上搜索资料,发现monkey代码中有一段代码可以接受activity启动和resume事件。最终满足要求。代码如下 1.设置观察者 mAm = ActivityManagerNative.getDefault(); try { mAm.setActivityController(new ActivityController()); } catch (RemoteException e) { System.err.println("** Failed talking with activity manager!"); } 2.观察者类 private class ActivityController extends IActivityController.Stub { //start public boolean activityStarting(Intent intent, String pkg) { currentPkg = pkg; mHandler.sendEmptyMessage(MSG_UPDATE_BACKGROUND); //currentIntent = intent; return true; } //resume public boolean activityResuming(String pkg) { currentPkg = pkg; mHandler.sendEmptyMessage(MSG_UPDATE_BACKGROUND); return true; } public int appEarlyNotResponding(String processName, int pid, String annotation){ return 0; } public boolean appCrashed(String processName, int pid, String shortMsg, String longMsg, long timeMillis, String stackTrace) { /* System.err.println("// CRASH: " + processName + " (pid " + pid + ")"); System.err.println("// Short Msg: " + shortMsg); System.err.println("// Long Msg: " + longMsg); System.err.println("// Build Label: " + Build.FINGERPRINT); System.err.println("// Build Changelist: " + Build.VERSION.INCREMENTAL); System.err.println("// Build Time: " + Build.TIME); System.err.println("// " + stackTrace.replace("\n", "\n// ")); if (!mIgnoreCrashes) { synchronized (Monkey.this) { mAbort = true; } return !mKillProcessAfterError; }*/ return false; } public int appNotResponding(String processName, int pid, String processStats) { // System.err.println("// NOT RESPONDING: " + processName + " (pid " + pid + ")"); //System.err.println(processStats); return 0; } 在一个service中加入以上代码,当activity活动时就会调用上述的方法packagename为activity的所在包名 由于ActivityManagerNative和IActivityController都属于内部类,所以该方法需要在源码环境下才可以使用 附一份IActivityController.aidl源码函数说明 package Android.app; import Android.content.Intent; /** * Testing interface to monitor what is happening in the activity manager * while tests are running. Not for normal application development. * {@hide} */ interface IActivityController { /** * The system is trying to start an activity. Return true to allow * it to be started as normal, or false to cancel/reject this activity. */ boolean activityStarting(in Intent intent, String pkg); /** * The system is trying to return to an activity. Return true to allow * it to be resumed as normal, or false to cancel/reject this activity. */ www.atcpu.com boolean activityResuming(String pkg); /** * An application process has crashed (in java). Return true for the * normal error recovery (app crash dialog) to occur, false to kill * it immediately. */ boolean appCrashed(String processName, int pid, String shortMsg, String longMsg, long timeMillis, String stackTrace); /** * Early call as soon as an ANR is detected. */ int appEarlyNotResponding(String processName, int pid, String annotation); /** * An application process is not responding. Return 0 to show the "app * not responding" dialog, 1 to continue waiting, or -1 to kill it * immediately. */ int appNotResponding(String processName, int pid, String processStats); } 作者:Androidchuxueze | |
![]() | ![]() |