2011年8月18日木曜日

Android Keygurad が外されたことを検知する

ロックを設定している場合、Intent.ACTION_SCREEN_ON だと画面がオンになった状態で通知がくるので、ロックが外れる前に通知がきてしまいます。画面がオンになったあと、ロックが外れた時に通知を受け取るには Intent.ACTION_USER_PRESENT を拾うようにします。

  1. public class UserPresentSampleActivity extends Activity {  
  2.   
  3.     private static final String TAG = "UserPresentSampleActivity";  
  4.       
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.     }  
  10.       
  11.     @Override  
  12.     protected void onResume() {  
  13.         super.onResume();  
  14.   
  15.         IntentFilter filter = new IntentFilter();  
  16.         filter.addAction(Intent.ACTION_USER_PRESENT);  
  17.         filter.addAction(Intent.ACTION_SCREEN_ON);  
  18.         filter.addAction(Intent.ACTION_SCREEN_OFF);  
  19.         registerReceiver(mReceiver, filter);  
  20.     };  
  21.   
  22.     @Override  
  23.     protected void onPause() {  
  24.         super.onPause();  
  25.           
  26.         unregisterReceiver(mReceiver);  
  27.     };  
  28.       
  29.     BroadcastReceiver mReceiver = new BroadcastReceiver() {  
  30.   
  31.         @Override  
  32.         public void onReceive(Context context, Intent intent) {  
  33.             String action = intent.getAction();  
  34.   
  35.             Log.d(TAG, "mReceiver received : " + action);  
  36.   
  37.             if(action.equals(Intent.ACTION_USER_PRESENT)){  
  38.             }  
  39.             else if(action.equals(Intent.ACTION_SCREEN_ON)){  
  40.             }  
  41.             else if(action.equals(Intent.ACTION_SCREEN_OFF)){                  
  42.             }  
  43.         }  
  44.     };      
  45. }  



D/UserPresentSampleActivity( 9858): mReceiver received : android.intent.action.SCREEN_ON
D/UserPresentSampleActivity( 9858): mReceiver received : android.intent.action.USER_PRESENT
D/UserPresentSampleActivity( 9858): mReceiver received : android.intent.action.SCREEN_OFF




0 件のコメント:

コメントを投稿