android 模擬物理按鍵
阿新 • • 發佈:2018-11-10
package com.example.sh04799.myapplication2; import android.app.Instrumentation; import android.util.Log; /** * Created by zhangwei on 2017/6/19. */ public class CustomFuncManager { private static CustomFuncManager mInstance; // 模擬物理按鍵onKey事件 private Instrumentation instrumentation = new Instrumentation();; public static CustomFuncManager getInstance() { if (mInstance == null) { mInstance = new CustomFuncManager(); } return mInstance; } public void sendKeyEvent(final int keyCode) { Thread thread = new Thread(new Runnable() { @Override public void run() { if (instrumentation != null) { instrumentation.sendKeyDownUpSync(keyCode); } else { Log.d("zw", "instrumentation is null"); } } }); thread.start(); } }