Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中屏蔽"電源鍵長按"、"Home鍵"、"Home鍵長按"

Android中屏蔽"電源鍵長按"、"Home鍵"、"Home鍵長按"

編輯:關於Android編程

Android中屏蔽"電源鍵長按"、"Home鍵"、"Home鍵長按"


這幾個“按鈕”的觸發,都會產生一個Action == Intent.ACTION_CLOSE_SYSTEM_DIALOGS的通知

區分他們三個的不同事件的參數,就是隨著Intent帶過來的reason字符串,分別對應

"電源鍵長按"(globalscreen)、"Home鍵"(homekey)、"Home鍵長按"(recentapps)

說破了就這麼簡單,直接上代碼了。

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) {
		// 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);
	}
}


效果圖:




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved