Android中屏蔽 电源键长按、Home键、Home长按


Android中屏蔽电源键长按、Home键、Home长按


 这几个“按键”的触发,都会产生一个 Action == Intent.ACTION_CLOSE_SYSTEM_DIALOGS 的通知区分他们三个的不同事件的参数,就是随着 Intent 带过来的 reason 字符串,分别对应:

"电源键长按"(globalscreen

"Home键"(homekey

"Home长按"(recentapps


我们可以使用广播来实现,如:

[java]  view plain copy print ?
  1. package com.jumpinus.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.IntentFilter;  
  8. import android.os.Bundle;  
  9.   
  10. public class MainActivity extends Activity {  
  11.       
  12.     private MyBroadCastReceiver myBroadCastReceiver;  
  13.       
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         // TODO Auto-generated method stub  
  17.         super.onCreate(savedInstanceState);  
  18.           
  19.           
  20.         myBroadCastReceiver = new MyBroadCastReceiver();  
  21.           
  22.     }  
  23.     @Override  
  24.     protected void onResume() {  
  25.         // TODO Auto-generated method stub  
  26.         super.onResume();  
  27.         IntentFilter intentFilter = new IntentFilter();  
  28.         intentFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);  
  29.           
  30.         System.out.println("I'm coming, myBroadCastReceiver注册了!");  
  31.         registerReceiver(myBroadCastReceiver, intentFilter);  
  32.     }  
  33.     private class MyBroadCastReceiver extends BroadcastReceiver{  
  34.   
  35.         @Override  
  36.         public void onReceive(Context context, Intent intent) {  
  37.             //你自己先把 reasons == homekey 和 长按homekey 排除,剩下的做下面的处理  
  38.             String reason = intent.getStringExtra("reason");  
  39.             if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){  
  40.                 System.out.println("Intent.ACTION_CLOSE_SYSTEM_DIALOGS : " + intent.getStringExtra("reason"));  
  41.                   
  42.                 if (intent.getExtras()!=null && intent.getExtras().getBoolean("myReason")){  
  43.                     myBroadCastReceiver.abortBroadcast();  
  44.                 }else if (reason != null){  
  45.                       
  46.                     if (reason.equalsIgnoreCase("globalactions")){  
  47.                           
  48.                         //屏蔽电源长按键的方法:  
  49.                         Intent myIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);  
  50.                         myIntent.putExtra("myReason"true);  
  51.                         context.sendOrderedBroadcast(myIntent, null);  
  52.                         System.out.println("电源  键被长按");  
  53.                           
  54.                     }else if (reason.equalsIgnoreCase("homekey")){  
  55.                           
  56.                         //屏蔽Home键的方法  
  57.                         //在这里做一些你自己想要的操作,比如重新打开自己的锁屏程序界面,这样子就不会消失了  
  58.                         System.out.println("Home 键被触发");  
  59.                           
  60.                     }else if (reason.equalsIgnoreCase("recentapps")){  
  61.                           
  62.                         //屏蔽Home键长按的方法  
  63.                         System.out.println("Home 键被长按");  
  64.                     }  
  65.                 }  
  66.             }  
  67.         }  
  68.           
  69.     }  
  70.       
  71.     @Override  
  72.     protected void onPause() {  
  73.         // TODO Auto-generated method stub  
  74.         super.onPause();  
  75.           
  76.         System.out.println("I get out, myBroadCastReceiver注销了!");  
  77.         unregisterReceiver(myBroadCastReceiver);  
  78.     }  
  79. }  



你可能感兴趣的:(Android技术笔记)