android 模拟物理按键

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ding1145536113/article/details/76461392
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();
    }

}

猜你喜欢

转载自blog.csdn.net/ding1145536113/article/details/76461392
今日推荐