音楽アプリなどで音楽再生中にスクリーンロックがかかった場合、
再度音楽を操作するのにいちいちスクリーンロックを解除するのはかなりの手間である。
Androidマーケットで常に上位のPowerAMPはこのように操作可能になっている。
このようなアプリではユーザビリティを考慮する上でも必要な機能となる。
そこで今回はスクリーンロック解除しなくても操作可能な画面&呼び出しを
ミニマム構成で作成してみた。
サンプルプロジェクトのダウンロードリンクは記事の最後。
構成は以下の通り。
- 画面の電源ONの通知を受け取るレシーバーの登録/登録解除を行うサービス
ScreenStateService
- スクリーンロック解除画面より手前に表示させる画面
ScreenLockEnabledActivity
- "2"の画面を表示させるか否かを設定する画面
SetActivity
画面の電源が入るとACTION_SCREEN_ONが通知される。
ACTION_SCREEN_ONを受け取るには明示的にregisterReceiverする必要がある。
ScreenStateService.java
- package jp.u1aryz.products.screenlockenable;
- import android.app.Service;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.IBinder;
- public class ScreenStateService extends Service {
- private BroadcastReceiver mScreenOnListener = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- // 画面の電源が入ったらActivityを起動
- if (action.equals(Intent.ACTION_SCREEN_ON)) {
- Intent i = new Intent(context, ScreenLockEnabledActivity.class);
- i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(i);
- }
- }
- };
- @Override
- public void onStart(Intent intent, int startId) {
- super.onStart(intent, startId);
- // ACTION_SCREEN_ONを受け取るBroadcastReceiverを登録
- IntentFilter filter = new IntentFilter();
- filter.addAction(Intent.ACTION_SCREEN_ON);
- registerReceiver(mScreenOnListener, filter);
- }
- @Override
- public void onDestroy() {
- // BroadcastReceiverを登録解除
- unregisterReceiver(mScreenOnListener);
- super.onDestroy();
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- }
かなり簡略化してシンプルな作りにしている。
そして今回のポイントとなる"Lock解除画面より手前に表示させる画面"を作成する。
ScreenLockEnabledActivity.java
- package jp.u1aryz.products.screenlockenable;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class ScreenLockEnabledActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.lock);
- // Lock解除画面より手前に表示させる
- final Window win = getWindow();
- win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
- | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
- Button btnRelease = (Button) findViewById(R.id.btn_release);
- btnRelease.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // Activityを終了することでLock解除画面に移る
- finish();
- }
- });
- }
- }
配置するといいと思う。
続いて"上記の画面を表示させるか否かを設定する画面"を作成する。
通常はPreferenceActivityなどで実装すると良い。
SetActivity.java
- package jp.u1aryz.products.screenlockenable;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- public class SetActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // チェックボックスの値を保存するために使用
- final SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE);
- final Intent intent = new Intent(SetActivity.this, ScreenStateService.class);
- CheckBox chbEnable = (CheckBox) findViewById(R.id.chb_enable);
- chbEnable.setChecked(pref.getBoolean("is_lockEnable", false));
- chbEnable.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- // チェックされたらサービスを起動
- if (isChecked) {
- pref.edit().putBoolean("is_lockEnable", isChecked).commit();
- startService(intent);
- // チェックが外されたらサービスを停止
- } else {
- pref.edit().putBoolean("is_lockEnable", isChecked).commit();
- stopService(intent);
- }
- }
- });
- }
- }
ダウンロードはこちらから