欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android实现检测实体按键事件并屏蔽

时间:2022-02-15 11:08:56|栏目: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;**加上注释。

上一篇:Android Studio3.0新特性及安装图文教程

栏    目:Android代码

下一篇:Android Handler移除Message详解及实例代码

本文标题:Android实现检测实体按键事件并屏蔽

本文地址:http://www.codeinn.net/misctech/193527.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有