Android亮屏、暗屏、解鎖、關閉系統對話的監聽事件

  1. protected void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.button_layuout);  
  4.   
  5.     final IntentFilter filter = new IntentFilter();  
  6.     // 屏幕滅屏廣播  
  7.     filter.addAction(Intent.ACTION_SCREEN_OFF);  
  8.     // 屏幕亮屏廣播  
  9.     filter.addAction(Intent.ACTION_SCREEN_ON);  
  10.     // 屏幕解鎖廣播  
  11.     filter.addAction(Intent.ACTION_USER_PRESENT);  
  12.     // 當長按電源鍵彈出“關機”對話或者鎖屏時系統會發出這個廣播  
  13.     // example:有時候會用到系統對話框,權限可能很高,會覆蓋在鎖屏界面或者“關機”對話框之上,  
  14.     // 所以監聽這個廣播,當收到時就隱藏自己的對話,如點擊pad右下角部分彈出的對話框   
  15.     filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);  
  16.   
  17.     BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {  
  18.         @Override  
  19.         public void onReceive(final Context context, final Intent intent) {  
  20.             Log.d(TAG, "onReceive");  
  21.             String action = intent.getAction();  
  22.   
  23.             if (Intent.ACTION_SCREEN_ON.equals(action)) {  
  24.                 Log.d(TAG, "screen on");  
  25.             } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {  
  26.                 Log.d(TAG, "screen off");  
  27.             } else if (Intent.ACTION_USER_PRESENT.equals(action)) {  
  28.                 Log.d(TAG, "screen unlock");  
  29.             } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {  
  30.                 Log.i(TAG, " receive Intent.ACTION_CLOSE_SYSTEM_DIALOGS");  
  31.             }  
  32.         }  
  33.     };  
  34.     Log.d(TAG, "registerReceiver");  
  35.     registerReceiver(mBatInfoReceiver, filter);  
  36. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章