Android中屏蔽 电源键长按、Home键、Home长按
2019-07-13 23:36发布
生成海报
Android中屏蔽电源键长按、Home键、Home长按
这几个“按键”的触发,都会产生一个 Action == Intent.ACTION_CLOSE_SYSTEM_DIALOGS 的通知区分他们三个的不同事件的参数,就是随着 Intent 带过来的 reason 字符串,分别对应:"电源键长按"(globalscreen)"Home键"(homekey)"Home长按"(recentapps)
我们可以使用广播来实现,如:[java] view plaincopyprint?- package com.jumpinus.test;
-
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
-
- public class MainActivity extends Activity {
-
- private MyBroadCastReceiver myBroadCastReceiver;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
-
-
- myBroadCastReceiver = new MyBroadCastReceiver();
-
- }
- @Override
- protected void onResume() {
-
- super.onResume();
- IntentFilter intentFilter = new IntentFilter();
- intentFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
-
- System.out.println("I'm coming, myBroadCastReceiver注册了!");
- registerReceiver(myBroadCastReceiver, intentFilter);
- }
- private class MyBroadCastReceiver extends BroadcastReceiver{
-
- @Override
- public void onReceive(Context context, Intent intent) {
-
- String reason = intent.getStringExtra("reason");
- if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
- System.out.println("Intent.ACTION_CLOSE_SYSTEM_DIALOGS : " + intent.getStringExtra("reason"));
-
- if (intent.getExtras()!=null && intent.getExtras().getBoolean("myReason")){
- myBroadCastReceiver.abortBroadcast();
- }else if (reason != null){
-
- if (reason.equalsIgnoreCase("globalactions")){
-
-
- Intent myIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
- myIntent.putExtra("myReason", true);
- context.sendOrderedBroadcast(myIntent, null);
- System.out.println("电源 键被长按");
-
- }else if (reason.equalsIgnoreCase("homekey")){
-
-
-
- System.out.println("Home 键被触发");
-
- }else if (reason.equalsIgnoreCase("recentapps")){
-
-
- System.out.println("Home 键被长按");
- }
- }
- }
- }
-
- }
-
- @Override
- protected void onPause() {
-
- super.onPause();
-
- System.out.println("I get out, myBroadCastReceiver注销了!");
- unregisterReceiver(myBroadCastReceiver);
- }
- }
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮