音声エディタを通知領域(ステータスバー)に常駐させるようにしたので、そのやり方などを書いておこうかと。
通知はNotificationクラスを使います。
アプリの常駐は、このNotificationのインスタンスのflagsにFLAG_ONGOING_EVENTを設定するというだけです。これだけで常駐アプリになります(実行中になる)。
// 通知領域に常駐させる
private void setNotification() {
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, getString(R.string.app_name), System.currentTimeMillis());
Intent intent = new Intent(this, VoiceEditor.class);
notification.flags = Notification.FLAG_ONGOING_EVENT; // 常駐
PendingIntent contextIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(getApplicationContext(), getString(R.string.app_name), getString(R.string.notify_summary), contextIntent);
notificationManager.notify(R.string.app_name, notification);
}
このままだと常に常駐するアプリになってしまうので(←言葉が変ですが)、PreferenceActivityとか使って常駐させるかどうかの設定をユーザにしてもらうほうが親切ですね。音声エディタはそのようにしています。
ちなみに常駐解除は以下。
private void cancelNotification() {
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(R.string.app_name);
}
また、通知領域からアプリを起動した場合と普通のアイコンからActivityを起動した場合、アプリが複数起動してしまいます。
もし同じアプリは1つしか起動してほしくないよ!という方はマニフェストファイルのActivityの設定項目で、
android:launchMode=”singleTask”
を設定しておきましょう!これで幸せになれます。

Pingback: 通知領域からActivityに復帰する方法 | choboti
Pingback: 「アプリを通知領域に常駐させる」の参考サイト | choboti