Android 入力コマンド シミュレーション イベントとイベント インジェクションの実装

コマンドパラメータを入力

adb シェルに入り、inputコマンドのヘルプを表示するには次のように入力します。

127|emulator_x86_64:/ # input                                                  
Usage: input [<source>] [-d DISPLAY_ID] <command> [<arg>...]

The sources are: 
      touchnavigation
      touchscreen
      joystick
      stylus
      touchpad
      gamepad
      dpad
      mouse
      keyboard
      trackball

-d: specify the display ID.
      (Default: -1 for key event, 0 for motion event if not specified.)
The commands and default sources are:
      text <string> (Default: touchscreen)
      keyevent [--longpress|--doubletap] <key code number or name> ... (Default: keyboard)
      tap <x> <y> (Default: touchscreen)
      swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
      draganddrop <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
      press (Default: trackball)
      roll <dx> <dy> (Default: trackball)
      motionevent <DOWN|UP|MOVE|CANCEL> <x> <y> (Default: touchscreen)
      keycombination [-t duration(ms)] <key code 1> <key code 2> ... (Default: keyboard, the key order is important here.)

コード パス: Frameworks/base/services/core/java/com/android/server/input/InputShellCommand.java
例:adb shell input tap 500 100
座標 (500,100) の位置でクリック イベントをシミュレートします。
タップ クリック イベントは、ソース コードに対応します。

    private void sendTap(int inputSource, float x, float y, int displayId) {
    
    
        final long now = SystemClock.uptimeMillis();
        injectMotionEvent(inputSource, MotionEvent.ACTION_DOWN, now, now, x, y, 1.0f,
                displayId);
        injectMotionEvent(inputSource, MotionEvent.ACTION_UP, now, now, x, y, 0.0f, displayId);
    }

パラメータ分析:
int inputSource: シミュレーション イベントのタイプを示します
float x、float y: イベントの座標点を示します
int displayId: イベントがトリガーされる画面 (通常は複数の画面で使用されます)

タッチイベントの挿入

        btn.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                Log.i("TAG:","onClick");
                injectClick();
            }
        });
        void injectClick() {
    
    
        Log.i("TAG:","injectClick");
        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                Instrumentation instrumentation = new Instrumentation();
                final long now = SystemClock.uptimeMillis();
                int action = MotionEvent.ACTION_DOWN;
                float x = 500;
                float y = 100;
                MotionEvent clickDown = MotionEvent.obtain(now,now,action,x,y,0);
                instrumentation.sendPointerSync(clickDown);
                action = MotionEvent.ACTION_UP;
                MotionEvent clickUp = MotionEvent.obtain(now,now,action,x,y,0);
                instrumentation.sendPointerSync(clickUp);
            }
        }).start();
    }

ボタンをクリックし、座標 (500,100) で対応するイベントをクリックします。

キーイベントの挿入

        btn.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                Log.i("TAG:","onClick");
                injectHome();
            }
        });
		void injectHome() {
    
    
		        Log.i("TAG:","injectHome");
		        new Thread(new Runnable() {
    
    
		            @Override
		            public void run() {
    
    
		                Instrumentation instrumentation = new Instrumentation();
		                final long now = SystemClock.uptimeMillis();
		                int action = KeyEvent.ACTION_DOWN;
		                KeyEvent homeDown = new KeyEvent(action,KeyEvent.KEYCODE_HOME);
		                instrumentation.sendKeySync(homeDown);
		                action = KeyEvent.ACTION_UP;
		                KeyEvent homeUp = new KeyEvent(action,KeyEvent.KEYCODE_HOME);
		                instrumentation.sendKeySync(homeUp);
		
		            }
		        }).start();
		    }

ボタンをクリックすると、対応するHOMEイベントが表示されます

イベント挿入権限の問題

また、私たちが作成したアプリケーションのエラーを報告する許可も与えられています。

ここに画像の説明を挿入します

AndroidManifest ソース コードを表示する Frameworks/base/core/res/AndroidManifest.xml

    <!-- @SystemApi Allows an application to inject user events (keys, touch, trackball)
         into the event stream and deliver them to ANY window.  Without this
         permission, you can only deliver events to windows in your own process.
         <p>Not for use by third-party applications.
         @hide
    -->
    <permission android:name="android.permission.INJECT_EVENTS"
        android:protectionLevel="signature" />

ソース コードから、サードパーティ アプリケーションがこの権限を使用できないことがわかります。

権限の問題を修正する

組み込みアプリケーションを作成して権限の問題を解決する
1. package/apps/ ディレクトリに対応するフォルダー (MyInject など) を作成します。
2. Android Studio で APK をビルドし、mk ファイルを作成し、
ここに画像の説明を挿入します
構成用にこのフォルダーに置きます。 mkファイル

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyInject
LOCAL_MODULE_TAGS := optional 
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
#LOCAL_CERTIFICATE := PRESIGNED
LOCAL_CERTIFICATE := platform
include $(BUILD_PREBUILT)

3.apkをコンパイルする

. build/envsetup.sh
lunch sdk_phone_x86_64 #must this
make MyInject

4.
ビルドされたapkをout/target/product/emulator_x86_64/system/app/MyInject/ディレクトリにインストールし
adb install MyInject.apkインストールします

おすすめ

転載: blog.csdn.net/yimelancholy/article/details/130496623