`
mars5337
  • 浏览: 89329 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

注入keyEvent 参考

阅读更多
Am also facing the same problem, can tell me wheather you got the
solution for the problem or not?


On Nov 22 2008, 5:23 am, "Dianne Hackborn" <hack...@android.com>
wrote:
> Correct, one application can not inject key events into another
> application.  There should be no way around this.
>
> For instrumentation test cases that cross application boundaries, I strongly
> recommend you set up an ActivityMonitor to block the launching of that other
> application's activity, returning a mocked result instead.
>
> On Fri, Nov 21, 2008 at 3:34 PM, dreamerBoy <paul.hube...@gmail.com> wrote:
>
> > Hi Hackbod -
>
> > I tried this using Instrumentation -
>
> > The goal of this little program is to make an outgoing call and then
> > generate a keypress on the ENDCALL button.
>
> > 1. It appears that I am incapable of unlocking the keyguard:
>
> > 11-21 14:40:58.445: INFO/InstTest(209): after
> > inKeyguardRestrictedInputMode() ? true
>
> > 2. Then, Android tells me I don't have permission to send a key event:
>
> > 11-21 14:41:00.173: WARN/WindowManager(53): Permission denied:
> > injecting key event from pid 209 uid 10019 to window Window{43506808
> > com.android.phone/com.android.phone.InCallScreen} owned by uid 1001
>
> > EmptyActivity is just as it sounds - no additional code other than
> > what Eclipse generates.
>
> > If anyone can see something that is not being done correctly, I would
> > very much appreciate their thoughts -
>
> > Thanks -
>
> > Paul
>
> > ~~~~~~~~~~
>
> > package test.instTest;
>
> > import android.app.Activity;
> > import android.content.Context;
> > import android.content.Intent;
> > import android.net.Uri;
> > import android.telephony.PhoneStateListener;
> > import android.telephony.TelephonyManager;
> > import android.test.ActivityInstrumentationTestCase;
> > import android.util.Log;
> > import android.view.KeyEvent;
> > import android.app.Instrumentation;
> > import android.view.View;
> > import android.content.IntentFilter;
> > import android.app.Instrumentation.ActivityResult;
> > import android.app.KeyguardManager;
>
> > public class InstTest extends
> > ActivityInstrumentationTestCase<EmptyActivity>
> > {
> >   private static final String LOG_TAG    = "InstTest";
> >   private TelephonyManager    telMgr;
> >   private Instrumentation     instrumentation;
> >   private Context             context;
> >   private KeyguardManager     keyguardMgr;
>
> >   public InstTest()
> >   {
> >      super("test.instTest", EmptyActivity.class);
> >   }
>
> >   public void testPreconditions()
> >   {
> >      instrumentation = getInstrumentation();
> >      assertTrue("Instrumentation must be non-null", instrumentation !
> > = null);
> >      context = instrumentation.getContext();
> >      assertTrue("Context must be non-null", context != null);
> >      telMgr = (TelephonyManager) context.getSystemService
> > (Context.TELEPHONY_SERVICE);
> >      assertTrue("TelephonyManager must be non-null", telMgr != null);
> >      keyguardMgr = (KeyguardManager) context.getSystemService
> > (Context.KEYGUARD_SERVICE);
> >      assertTrue("KeyguardManager must be non-null", keyguardMgr !=
> > null);
> >   }
>
> >   public void testCall()
> >   {
> >      testPreconditions();
>
> >      Log.i(LOG_TAG, "before inKeyguardRestrictedInputMode() ? " +
> > keyguardMgr.inKeyguardRestrictedInputMode());
>
> >      KeyguardManager.KeyguardLock keyguardLock =
> > keyguardMgr.newKeyguardLock(LOG_TAG);
> >      keyguardLock.disableKeyguard();
>
> >      Log.i(LOG_TAG, "after inKeyguardRestrictedInputMode() ? " +
> > keyguardMgr.inKeyguardRestrictedInputMode());
>
> >      IntentFilter intentFilter = new IntentFilter
> > (Intent.ACTION_CALL);
>
> >      Uri parsedPhoneNumber = Uri.parse("tel:1234567");
>
> >      Intent myIntent = new Intent(Intent.ACTION_CALL,
> > parsedPhoneNumber);
> >      Intent resultData = new Intent(Intent.ACTION_CALL,
> > parsedPhoneNumber);
> >      // myIntent = new Intent(Intent.ACTION_DIAL, parsedPhoneNumber);
>
> >      myIntent.setFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION |
> > Intent.FLAG_FROM_BACKGROUND
> >            | Intent.FLAG_ACTIVITY_SINGLE_TOP |
> > Intent.FLAG_ACTIVITY_NEW_TASK);
> >      resultData.setFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION |
> > Intent.FLAG_FROM_BACKGROUND
> >            | Intent.FLAG_ACTIVITY_SINGLE_TOP |
> > Intent.FLAG_ACTIVITY_NEW_TASK);
>
> >      Instrumentation.ActivityResult actResult = new
> > Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
>
> >      Instrumentation.ActivityMonitor actMonitor = new
> > Instrumentation.ActivityMonitor(intentFilter, actResult, false);
>
> >      Log.i(LOG_TAG, "starting call.");
>
> >      instrumentation.waitForIdleSync();
>
> >      context.startActivity(myIntent);
>
> >      instrumentation.waitForIdleSync();
>
> >      Log.i(LOG_TAG, "number of hits from ActivityMonitor: " +
> > actMonitor.getHits());
> >      Activity phoneActivity = actMonitor.getLastActivity();
> >      if (phoneActivity != null)
> >         Log.i(LOG_TAG, "phoneActivity is NOT NULL!!");
> >      else
> >         Log.i(LOG_TAG, "phoneActivity is NULL");
>
> >      Log.i(LOG_TAG, "before phone state is " + phoneStateToString());
>
> >      Activity activity = getActivity();
> >      Log.i(LOG_TAG, "activity class is " + activity.getClass());
>
> >      View view = getActivity().getCurrentFocus();
> >      if (view == null)
> >         Log.i(LOG_TAG, "Focus view is NULL");
> >      else
> >         Log.i(LOG_TAG, "Focus view is NOT NULL");
>
> >      Log.i(LOG_TAG, "Sending ENDCALL");
> >      sendKeys(KeyEvent.KEYCODE_ENDCALL);
>
> >      instrumentation.waitForIdleSync();
> >      Log.i(LOG_TAG, "Sent ENDCALL, sleeping");
>
> >      sleep(1000);
>
> >      Log.i(LOG_TAG, "after phone state is " + phoneStateToString());
> >   }
> > }
>
> > <?xml version="1.0" encoding="utf-8"?>
> > <manifest xmlns:android="http://schemas.android.com/apk/res/android";
> >        package="test.instTest" android:versionCode="1"
> > android:versionName="1.0.0">
> >        <application android:icon="@drawable/icon" android:label="@string/
> > app_name">
> >                <uses-library android:name="android.test.runner" />
> >                <activity android:name=".EmptyActivity"
> > android:label="@string/
> > app_name">
> >                        <intent-filter>
> >                                <action
> > android:name="android.intent.action.MAIN" />
> >                                <category
> > android:name="android.intent.category.LAUNCHER" />
> >                        </intent-filter>
> >                </activity>
> >        </application>
> >        <instrumentation
> > android:name="android.test.InstrumentationTestRunner"
> >                android:targetPackage="test.instTest" android:label="first
> > phone key
> > test" />
> >        <uses-permission
> > android:name="android.permission.CALL_PHONE"></uses-
> > permission>
> >        <uses-permission
> > android:name="android.permission.READ_PHONE_STATE"></
> > uses-permission>
> >        <uses-permission
> > android:name="android.permission.DISABLE_KEYGUARD"></
> > uses-permission>
> > </manifest>
>
> > >adb shell am instrument -w -e class test.instTest.InstTest#testCall
> > test.instTest/android.test.InstrumentationTestRunner
>
> > test.instTest.InstTest:.
> > Test results for InstTest=.
> > Time: 3.368
>
> > OK (1 test)
>
> > On Nov 13, 4:37 pm, hackbod <hack...@gmail.com> wrote:
> > > Fwiw, there has never been a public API to inject a key event, except
> > > for the official API is on the Instrumentation class (which still
> > > exists).  As far as I know we don't right now have an API to get an
> > > Instrumentation object when not running an instrumentation test,
> > > though.
>
> > > I am a little confused about what you are asking -- you say that you
> > > want to send an ENDCALL to end a call, but that this is inside of the
> > > same application?  I believe that if your app currently has focus, the
> > > system will process that end call, though this is really not by
> > > design, but is probably not a big deal (we decide whether it is okay
> > > based on which window has focus so will be receiving the event...  but
> > > end call is special since it is intercepted by the system and never
> > > delivered to the focused window).  But if the user is in the in-call
> > > screen, then that has focus, and you can't inject a key event since
> > > you don't have focus.
>
> > > On Nov 13, 4:13 pm,dreamerBoy<paul.hube...@gmail.com> wrote:
>
> > > > I'm building a test application and I have to be able to hang up a
> > > > call in an automated fashion.
>
> > > > It occurred to me that I might be able to inject a key event:
>
> > > >       KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
> > > > KeyEvent.KEYCODE_ENDCALL );
>
> > > > into the event queue somehow.
>
> > > > Apparently the last incarnation of the  API had an injectKeyEvent()
> > > > method in WindowManager but that's been stripped out.
>
> > > > Anyone know how to do it in 1.0?
>
> > > > Thanks much.
>
> > > > dreamer
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com
>
> Note: please don't send private questions to me, as I don't have time to
> provide private support.  All such questions should be posted on public
> forums, where I and others can see and answer them.
分享到:
评论

相关推荐

    Robot键盘常量java.awt.event.KeyEvent。KeyEvent全部键盘按键映射整理KeyEvent枚举类

    Robot键盘常量java.awt.event.KeyEvent。KeyEvent全部键盘按键映射整理KeyEvent枚举类

    Android 9.0 10.0 framework添加KeyEvent按键事件流程.rar

    在Android系统中,KeyEvent是处理用户输入硬件按键事件的关键类,它封装了按键按下和释放的动作。...Android 10.0 KeyEvent按键添加流程.txt文件应该详细记录了这一过程的步骤和注意事项,供开发者参考。

    keyevent.util.zip

    "keyevent.util.zip"这个文件提供了一种JavaScript的解决方案,用于在Web应用程序中识别和处理来自不同平台(如联通IPTV和广电DVB)的遥控器输入。下面将详细介绍这个知识点及其相关技术。 1. **JavaScript事件处理...

    安卓 adb shell input keyevent 按键大全.txt

    在深入探讨安卓ADB Shell Input Keyevent按键大全之前,我们首先需要理解ADB(Android Debug Bridge)的概念及其作用。ADB是Google开发的一款用于与Android设备进行通信的工具,它提供了丰富的命令行选项来控制和...

    Android keyevent按键与对应的值

    该文档包含了Android中常用的210个按键及对应的值数据。

    Android 跨进程模拟按键(KeyEvent )实例详解

    本文将详细讲解如何实现Android跨进程模拟按键(KeyEvent)。 首先,我们来看一个基本的发送按键事件的代码片段: ```java public static void simulateKeystroke(final int keyCode) { new Thread(new Runnable...

    android keyevent事件

    ### Android KeyEvent事件详解 #### 一、KeyEvent事件概述 在Android系统中,KeyEvent事件主要用于处理用户的按键输入。这种事件在很多场景中都有应用,比如实体按键(如返回键、音量键等)以及虚拟按键(如软键盘...

    android_KeyEvent

    而KeyEvent类的CREATOR字段是一个Parcelable.Creator&lt;KeyEvent&gt;类型的实例,它可以用于在Intent中传递KeyEvent对象。 具体到代码层面,当开发者需要监听键盘事件时,可以在Activity中重写dispatchKeyEvent方法,...

    Android中三种注入事件方法比较

    本文主要探讨了三种在Android中注入事件的方法:使用内部APIs、使用`Instrumentation`对象以及直接向设备的`/dev/input/eventX`路径注入事件。下面我们将详细解析这三种方法的优缺点以及具体实现。 **方法1:使用...

    利用monkey注入事件原理实现Android 事件注入

    本主题将深入探讨“利用monkey注入事件原理实现Android事件注入”的过程,以及如何结合反射和蓝牙技术来实现远程控制。 首先,Monkey是Android SDK自带的一个命令行工具,它可以向系统发送伪随机的用户事件流,如...

    Android事件注入Demo

    在Android开发中,事件注入是一种技术,用于在应用程序运行时动态地模拟用户输入,以便测试或调试特定功能。本文将围绕“Android事件注入Demo”展开,深入探讨这一主题,并结合提供的资源“android-inputinjector-...

    Android本地注入事件

    6. **JNI(Java Native Interface)**:在需要更底层操作,比如直接与Linux内核交互时,开发者可能会使用JNI来编写C/C++代码,直接调用Linux系统调用来注入事件。 7. **安全与权限**:由于事件注入可能被滥用,如恶意...

    adb shell input keyevent 控制按键输入的数值(收藏版)

    `adb shell input keyevent` 是Android开发者调试和自动化测试中常用的一个命令,它允许你在设备或模拟器上模拟各种按键事件。这个工具是Android Debug Bridge(ADB)的一部分,通过这个命令,开发者可以在命令行...

    react-native-keyevent:捕获外部键盘键或远程控制按钮事件

    React本机KeyEvent 捕获外部键盘按键或远程控制按钮事件 。 安装 通过npm 运行npm install react-native-keyevent --save 通过纱线 运行yarn add react-native-keyevent 连结中 Android: react-native link ...

    安卓模拟系统发送返回键值(KEYCODE_BACK)(csdn)————程序.pdf

    关于权限`&lt;uses-permission android:name="android.permission.INJECT_EVENTS" /&gt;`,这是注入事件所需的权限。通常情况下,系统应用或具有签名级别权限的应用才有权使用此权限。对于普通应用,如果不使用`...

    Android代码模拟按键事件

    KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_1); ``` 这里的`ACTION_DOWN`表示按键被按下,`KEYCODE_1`是表示数字键1的常量。如果要模拟按键释放,可以将`ACTION_DOWN`替换为`ACTION...

    Android虚拟键码对照表

    在Android操作系统中,虚拟键码(Key Codes)是系统识别硬件按键输入的一种方式。这些键码定义了用户可能在触摸屏、物理键盘或其他输入设备上按下的一系列按键。...这份文档是Android开发者不可或缺的参考资料。

    RK3128平台android系统添加修改遥控器键值参考文档

    RK3128平台android系统添加修改遥控器键值参考文档,input.h中定义的键值在KeyEvent.java中不一定有定义哦,如果需要添加新的键值,可参考KeyEvent.java中添加新键值的那段注释。

    SimulateKey:Simulate Android KeyEvent,Android按键模拟器

    KeyEvent event = new KeyEvent( System.currentTimeMillis(), // 时间戳 SystemClock.uptimeMillis(), // 上升沿时间 KeyEvent.ACTION_DOWN, // 按下动作 KeyEvent.KEYCODE_A, // 键码 0, // 重复次数 0, // ...

    Android 实现按两次返回键退出程序(两种方法)

    Android 实现按两次返回键退出程序(两种方法...public boolean onKeyUp(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (isExit == false) { isExit = true; if (tExit != null) {

Global site tag (gtag.js) - Google Analytics