1. 程式人生 > >android 模擬物理按鍵

android 模擬物理按鍵

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();
    }

}