Google Developers Live
[JP 日本語] Google Play での Android アプリ提供ことはじめ
22分40秒あたり
コード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.app.KeyguardManager; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.os.Bundle; | |
import android.util.Log; | |
public class MainActivity extends Activity { | |
private final String TAG = getClass().getSimpleName(); | |
private MyBroadcastReceiver mReceiver = new MyBroadcastReceiver(); | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
protected void onResume() { | |
Log.d(TAG, "onResume()"); | |
super.onResume(); | |
IntentFilter intentFilter = new IntentFilter(); | |
intentFilter.addAction(Intent.ACTION_USER_PRESENT); | |
registerReceiver(mReceiver, intentFilter); | |
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); | |
Log.d(TAG, "inKeyguardRestrictedInputMode() : " + keyguardManager.inKeyguardRestrictedInputMode()); | |
if (!keyguardManager.inKeyguardRestrictedInputMode()) { | |
Log.d(TAG, "再開"); | |
/* | |
* 再開処理 | |
*/ | |
} | |
} | |
@Override | |
protected void onPause() { | |
Log.d(TAG, "onPause()"); | |
super.onPause(); | |
unregisterReceiver(mReceiver); | |
Log.d(TAG, "中断"); | |
/* | |
* 中断処理 | |
*/ | |
} | |
private class MyBroadcastReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.d(TAG, "MyBroadcastReceiver#onReceive()"); | |
Log.d(TAG, "再開"); | |
/* | |
* 再開処理 | |
*/ | |
} | |
} | |
} |
まとめ
- アプリが開いている状態でスリープから復帰した場合、ロックスクリーンが表示されていても、onResume()が呼ばれる
- ロックスクリーンが表示されているかの判定はKeyguardManager#inKeyguardRestrictedInputMode()で可能
- ロックスクリーンを使用していない場合への考慮も必要
- ロックスクリーン解除時は、ブロードキャストインテントandroid.intent.action.USER_PRESENTがシステムから通知される
その他
KeyguardManagerですが、API Level 16からisKeyguardLocked()/isKeyguardSecure()が追加されたようです。
isKeyguardLocked()でもロックスクリーン表示の判断が行えました。
isKeyguardSecure()はロックスクリーンにパスワードやパターンなどが設定されている時にtrueを返すようです。
※ API Level 15のIS11LGでも上記のメソッドが使用出来ました。リファレンスの誤記でしょうか?
参考にしたサイト
Yukiの枝折
Android:キーガードはActivityではなくViewであることの影響
0 件のコメント:
コメントを投稿