Android實現檢測實體按鍵事件並屏蔽

本文實例為大傢分享瞭Android實現檢測實體按鍵事件並屏蔽的具體代碼,供大傢參考,具體內容如下

效果圖:

新建服務類MyKeyEventService.java。用於監聽電源鍵的觸發。

public class MyKeyEventService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        final BroadcastReceiver mReceiver = new MainActivity.ScreenReceiver();
        registerReceiver(mReceiver, filter);
        return super.onStartCommand(intent, flags, startId);
    }

    public class LocalBinder extends Binder {
        MyKeyEventService getService() {
            return MyKeyEventService.this;
        }
    }

}

註冊服務類,AndroidManifest.xml文件。

<service android:name=".MyKeyEventService">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</service>

主界面MainActivity.java。

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MyKeyEvent";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this, LockService.class));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(new Intent(MainActivity.this, LockService.class));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                Log.i(TAG, "onKeyDown: vol+ down");
                return true;
//                break;

            case KeyEvent.KEYCODE_VOLUME_DOWN:
                Log.i(TAG, "onKeyDown: vol- down");
                return true;
//                break;

            case KeyEvent.KEYCODE_POWER:
                Log.i(TAG, "onKeyDown: power down");
                return true;
//                break;

            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                Log.i(TAG, "onKeyDown: vol+ up");
                return true;
//                break;

            case KeyEvent.KEYCODE_VOLUME_DOWN:
                Log.i(TAG, "onKeyDown: vol- up");
                return true;
//                break;

            case KeyEvent.KEYCODE_POWER:
                Log.i(TAG, "onKeyDown: power up");
                return true;
//                break;

            default:
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    static class ScreenReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(final Context context, final Intent intent) {
//            Log.e(TAG, "onReceive " + intent.getAction());
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                // do whatever you need to do here
                Log.e(TAG, "Screen Off");

            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                // and do whatever you need to do here
                Log.e(TAG, "Screen On");

            }
        }
    }

}

如果不想屏蔽系統按鍵事件,則把**return true;**加上註釋。

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: