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

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
这几个“按键”的触发,都会产生一个 Action == Intent.ACTION_CLOSE_SYSTEM_DIALOGS 的通知区分他们三个的不同事件的参数,就是随着  Intent 带过来的  reason 字符串,分别对应:

"电源键长按"(globalscreen)

"Home键"(homekey)

"Home键长按"(recentapps)

 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) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
          
          
        myBroadCastReceiver = new MyBroadCastReceiver();  
          
    }  
    @Override  
    protected void onResume() {  
        // TODO Auto-generated method stub  
        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) {  
            //你自己先把 reasons == homekey 和 长按homekey 排除,剩下的做下面的处理  
            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")){  
                          
                        //屏蔽Home键的方法  
                        //在这里做一些你自己想要的操作,比如重新打开自己的锁屏程序界面,这样子就不会消失了  
                        System.out.println("Home 键被触发");  
                          
                    }else if (reason.equalsIgnoreCase("recentapps")){  
                          
                        //屏蔽Home键长按的方法  
                        System.out.println("Home 键被长按");  
                    }  
                }  
            }  
        }  
          
    }  
      
    @Override  
    protected void onPause() {  
        // TODO Auto-generated method stub  
        super.onPause();  
          
        System.out.println("I get out, myBroadCastReceiver注销了!");  
        unregisterReceiver(myBroadCastReceiver);  
    }  
}  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Java版2048游戏实现代码

下一篇:简单实用的时间处理Java工具类DateUtil