Android ActivityManager isUserAMonkey() 方法探究 与 monkey 测试

这个方法不论是从表面还是从系统源码代分析来看他都是google开的一个玩笑!

直到你百度 monkey 测试,哦你才恍然大悟,原来是 猴子测试


何为monkey 测试?

顾名思义,让猴子帮你测试。听起来是不是很扯?

其实当你需要对你的应用进行暴力测试时,显然一只猴子会比你做的更专业。方法很简单,把手机扔给猴子,10分钟后拿回,如果你的应用没有FC,手机没死机,那么恭喜你,你的技术很牛X,连猴子都找不到BUG!


哈哈,开玩笑啦,你真以为猴子也会切水果么?他只是看中了香蕉而已!


正题:

adb shell

monkey -p com.mrpoid.shequ2 500

执行这条命令后,你的手机已经被猴子抓了500次了,快看看成什么样了!


------------------------------------------------------------------------------------------------------------

isUserAMonkey()

Returns "true" if the user interface is currently being messed with by a monkey.
字面意思为 返回 true 如果此时用户界面被一直猴子给搞乱了


api 文档看不出什么究竟,我们来看下源码吧

    public boolean isUserAMonkey() {
        // For now the fact that there is a controller implies
        // we have a monkey.
        synchronized (this) {
            return mController != null;
        }
    }

在看一个方法

    public void setActivityController(IActivityController controller) {
        enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
                "setActivityController()");
        synchronized (this) {
            mController = controller;
        }
    }


再看下这个 mController 在什么地方用到了吧

    public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) {
        // Refuse possible leaked file descriptors
        if (resultData != null && resultData.hasFileDescriptors() == true) {
            throw new IllegalArgumentException("File descriptors passed in Intent");
        }

        synchronized(this) {
            if (mController != null) {
                // Find the first activity that is not finishing.
                ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0);
                if (next != null) {
                    // ask watcher if this is allowed
                    boolean resumeOK = true;
                    try {
                        resumeOK = mController.activityResuming(next.packageName);
                    } catch (RemoteException e) {
                        mController = null;
                    }
    
                    if (!resumeOK) {
                        return false;
                    }
                }
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值