你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / 数据库开发
在源码中监听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.2cto.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

  推荐精品文章

·2024年12月目录 
·2024年11月目录 
·2024年10月目录 
·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089