本文介绍了安卓:java.lang.SecurityException异常:注射到另一个应用程序需要INJECT_EVENTS许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新来的Andr​​oid Junit的测试:

Hi there I am new to Android Junit testing:

我已经写了一些测试code在MainActivityFunctionalTest.java文件

I have written some test code in MainActivityFunctionalTest.java file

MainActivityFunctionalTest.java:

MainActivityFunctionalTest.java:

package com.example.myfirstapp2.test;

public class MainActivityFunctionalTest extends ActivityInstrumentationTestCase2<Login>{

private static final String TAG = "MainActivityFunctionalTest";
private Login activity;

  public MainActivityFunctionalTest() {
    super(Login.class);
  }


  @Override
  protected void setUp() throws Exception {
     Log.d(TAG,"Set-Up");
     super.setUp();
    setActivityInitialTouchMode(false);
    activity = getActivity();
  }

  public void testStartSecondActivity() throws Exception {
      // add monitor to check for the second activity
        ActivityMonitor monitor =
            getInstrumentation().
              addMonitor(DisplayMessageActivity.class.getName(), null, false);
        //addMonitor(MainActivity.class.getName(), null, false);
     // find button and click it
        Button view = (Button) activity.findViewById(R.id.btnLogin);

        // TouchUtils handles the sync with the main thread internally
        TouchUtils.clickView(this, view);

        // to click on a click, e.g., in a listview
        // listView.getChildAt(0);

        // wait 2 seconds for the start of the activity
        DisplayMessageActivity startedActivity = (DisplayMessageActivity) 

     monitor
            .waitForActivityWithTimeout(5000);
        assertNotNull(startedActivity);

        // search for the textView
        TextView textView = (TextView) startedActivity.findViewById(R.id.Email);

        // check that the TextView is on the screen
        ViewAsserts.assertOnScreen(startedActivity.getWindow().getDecorView(),
            textView);
        // validate the text on the TextView
        assertEquals("Text incorrect", "1http://www.vogella.com", 

         textView.getText().toString());

        // press back and click again
        this.sendKeys(KeyEvent.KEYCODE_BACK);

        TouchUtils.clickView(this, view);

  }


    }

不过,我得到一个错误:java.lang.SecurityException异常:注射到另一个应用程序需要INJECT_EVENTS许可

However,I get an error:java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission

在com.example.myfirstapp2.test.MainActivityFunctionalTest.testStartSecondActivity(MainActivityFunctionalTest.java:70)

at com.example.myfirstapp2.test.MainActivityFunctionalTest.testStartSecondActivity(MainActivityFunctionalTest.java:70)

 TouchUtils.clickView(this, view);

请帮忙

推荐答案

我正面临这同样的问题我自己,这里是我发现了这个问题。

I was facing this very same problem myself, and here is what I found about this issue.

1)添加INJECT_EVENTS许可,您的应用程序,让机器人工作室指出,这种许可仅授予系统应用程序。此外,Google's参考指南为manifest.permissions指出,这个权限是不为第三方应用程序使用。

1) Adding INJECT_EVENTS permission to your app, makes Android Studio point out that such permission "Is only granted to system apps". Moreover, Google's reference guide for manifest.permissions states that this permission is "Not for use by third-party applications."

现在,机会是您的应用程序,像我,是不是系统应用程序。因此,加入此权限绝对不是一件好事做了,幸运的是不会适用于你的第三方项目。至少在Android Studio的开发时。

Now, chances are that your app, as mine, is not a system app. So adding this permission is definitely not a good thing to do, and luckily will not be apply on your 3rd party project. At least when developing on Android Studio.

2)我可以看到,在你的安装方法,你叫 setActivityInitialTouchMode(假); 作为所指出的谷歌的最佳实践,测试UI时必须设置触摸模式为真的。否则,测试夹具不会能够与UI元素交互。

2) I can see that in your setUp method, you have called setActivityInitialTouchMode(false); As pointed out by Google's best practices for UI testing, when testing UI one must set Touch Mode to true. Otherwise, your test fixture will not be able to interact with UI elements.

3)还有一件事。这是一个自动化测试,在您的应用程序模拟用户操作。如果我们与设备(真实或虚拟的,无所谓)互动时,我们将最有可能做其他的事情获得焦点(甚至在被测试的应用程序),然后会出现与触摸模式的设置使得建立方法的冲突已经执行的。

3) Just one more thing. This is an automated test that emulates user actions upon your app. If we interact with the device (real or virtual, doesn't matter), we will most likely make other things gain focus (even inside the app under test) and then there will be a conflict with the touch mode settings that the setUp method had performed.

最后,这是发生了什么事给我。我简单地解决我的问题通过不点击/触摸/与设备交互当测试正在运行的时间。

Ultimately, that is what was happening to me. I solved my problem simply by not clicking/touching/interacting with the device where the test was being ran.

这篇关于安卓:java.lang.SecurityException异常:注射到另一个应用程序需要INJECT_EVENTS许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:52