在源碼中監聽activity啟動

上周做一個小的功能,修改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.aiwalls.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
 

 

發佈留言