【问题标题】:Android check if native activity has startedAndroid 检查本机活动是否已启动
【发布时间】:2014-12-06 10:24:22
【问题描述】:

我正在尝试测试我的应用程序是否已正确启动 Android Dialer 应用程序。

根据我对单元测试和仪器的发现,我创建了这个测试:

public void testPerformOperation() throws Exception {
    Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor("com.android.phone.Dialer", null, true);
    operationPerformer.performOperation("tel:111222333");
    Activity activity = monitor.waitForActivityWithTimeout(5000);
    assertNotNull(activity);
}

但这总是返回 AssertionFailedError。

我也尝试将addMonitor() 的第一个参数设置为IntentFilter.create(Intent.ACTION_DIAL, "vnd.android.cursor.dir/calls"),但这给了我同样的错误。另外,我一直在寻找其他要使用的 MimeType,但没有找到(看起来 dialer 没有使用 MimeType)。

我应该如何检查拨号器是否已启动?

【问题讨论】:

    标签: android unit-testing android-instrumentation


    【解决方案1】:

    检查您的活动是否正在运行

    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            for (RunningServiceInfo act: manager
                    .getRunningServices(Integer.MAX_VALUE)) {
                if (MyActivity.class.getName().equals(
                        act.service.getClassName())) {
    
                    //do
                    return true;
                }
            }
    

    【讨论】:

    • 好的,但我没有或找不到适合本机拨号器应用的活动名称。
    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多