活动监视器getHits()不工作 [英] ActivityMonitor getHits() doesn't work

查看:225
本文介绍了活动监视器getHits()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这个测试,但getHits()始终返回0。任何人都可以帮我吗?

 公共无效testSettingsAboutShazamClickOnLink(){    仪器仪表研究所= getInstrumentation();
    IntentFilter的IntentFilter的=新的IntentFilter(android.content.Intent.ACTION_SENDTO);
    intentFilter.addDataScheme(电子邮件地址);
    活动监视器监视器= inst.addMonitor(IntentFilter的,空,假);
    最终意图emailIntent =新意图(android.content.Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse(电子邮件地址:));
    emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    的assertEquals(0,monitor.getHits());
    。inst.getContext()startActivity(emailIntent);
    monitor.waitForActivityWithTimeout(5000);
    的assertEquals(1,monitor.getHits());
    inst.removeMonitor(显示器);
 }


解决方案

我解决了这个问题。我用Robotium的独奏和独唱显然插入每一个过滤器相匹配的活动监视器,所以当你调用startActivity,它在仪器仪表类调用execStartActivity,其code是

 上下文谁,的IBinder contextThread,令牌的IBinder,活动目标,
    意图意图,INT请求code){
    IApplicationThread whoThread =(IApplicationThread)contextThread;
    如果(mActivityMonitors!= NULL){
        同步(则msync){
            最终诠释N = mActivityMonitors.size();
            的for(int i = 0; I< N;我++){
                最终活动监视器上午= mActivityMonitors.get(ⅰ);
               如果(am.match(谁,空,意图)){
                   am.mHits ++;
                    如果(am.isBlocking()){
                        返回请求code> = 0? am.getResult():空;
                    }
                   打破;
                }
            }
       }
    }

一旦有匹配的东西,监视器的mHits增加末尾的循环断裂的活动监视器。鉴于调用独奏是我做的第一件事,第一个活动监视器检查是独奏的一种,因为这一切都匹配,它没有被选中后添加任何其他ActivityMonitors,所以没有mHits变量为其他显示器增加。如果您想了解更多的看到仪表类的execStartMonitor方法,记住startActivity被调用后该方法被调用。

<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/1.5_r4/android/app/Instrumentation.java#Instrumentation.execStartActivity%28android.content.Context%2Candroid.os.IBinder%2Candroid.os.IBinder%2Candroid.app.Activity%2Candroid.content.Intent%2Cint%29\" rel=\"nofollow\">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/1.5_r4/android/app/Instrumentation.java#Instrumentation.execStartActivity%28android.content.Context%2Candroid.os.IBinder%2Candroid.os.IBinder%2Candroid.app.Activity%2Candroid.content.Intent%2Cint%29

I tried this test but getHits() always return 0. Anyone can help me?

 public void testSettingsAboutShazamClickOnLink() {

    Instrumentation inst = getInstrumentation();
    IntentFilter intentFilter = new IntentFilter(android.content.Intent.ACTION_SENDTO);
    intentFilter.addDataScheme("mailto");


    ActivityMonitor monitor = inst.addMonitor(intentFilter, null, false);
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


    assertEquals(0, monitor.getHits());
    inst.getContext().startActivity(emailIntent);        
    monitor.waitForActivityWithTimeout(5000);
    assertEquals(1, monitor.getHits());
    inst.removeMonitor(monitor);
 }

解决方案

I solved this problem. I used Solo of Robotium and apparently Solo inserts an ActivityMonitor that matches every Filter, so when you call startActivity, it calls execStartActivity in the Instrumentation class, whose code is

 Context who, IBinder contextThread, IBinder token, Activity target,
    Intent intent, int requestCode) {
    IApplicationThread whoThread = (IApplicationThread) contextThread;
    if (mActivityMonitors != null) {
        synchronized (mSync) {
            final int N = mActivityMonitors.size();
            for (int i=0; i<N; i++) {
                final ActivityMonitor am = mActivityMonitors.get(i);
               if (am.match(who, null, intent)) {
                   am.mHits++;
                    if (am.isBlocking()) {
                        return requestCode >= 0 ? am.getResult() : null;
                    }
                   break;
                }
            }
       }
    }

As soon as there is an ActivityMonitor that matches something the mHits of that monitor is increased end the "cycle for" breaks. Given that calling Solo was the first thing I did, the first ActivityMonitor to check is the one of Solo, given that this matches everything, any other ActivityMonitors added after it is not checked, so no mHits variable is increased for the other monitors. If you want to understand more see the execStartMonitor method of Instrumentation class, remembering that this method is called after startActivity is called.

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/app/Instrumentation.java#Instrumentation.execStartActivity%28android.content.Context%2Candroid.os.IBinder%2Candroid.os.IBinder%2Candroid.app.Activity%2Candroid.content.Intent%2Cint%29

这篇关于活动监视器getHits()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆