androidのホームの上部にあるステータスバーをドラッグすると、下のSSのように通知領域が表示されます。

通知領域に情報を出すためには「NotificationManagerクラス」と「Notificationクラス」を使います。
参考:Creating Status Bar Notifications
この通知情報(notification)を選択すると、intentが発行され、intentに基づいた処理(アクティビティを表示させるなど)が行われます。バックグラウンドで動作しているサービスなどが、ユーザに対して情報を表示したい、選択を迫りたい場合などに使用されるのが一般的な使われ方かと思います。
《ステータスバーに情報を通知する例》
NotificationおよびNotificationManagerの使い方は例えば以下のようになります。
- まずNotificationManagerの参照を取得します。
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- Notificationクラスを生成します。
Notification notification = new Notification(
android.R.drawable.btn_default,
"通知情報が届きました",
System.currentTimeMillis());
- notificationが選択された場合に発行するintentをPendingIntentで包みます。
Intent intent = new Intent(Intent.ACTION_VIEW);
//intentの設定
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, intent, 0);
- 通知する情報をsetLatestEventInfoメソッドにて設定します。
notification.setLatestEventInfo(
getApplicationContext(),
"アプリ名",
"通知情報の説明文",
contentIntent);
- NotificationManagerでNotificationをnotifyします。
notificationManager.notify(R.string.app_name, notification);
ここまで行うと、画面上部に以下のように表示されます。

ドラッグして通知領域を確認すると以下のように表示されています。これを押下するとintentが発行されます。

《NotificationManager》
NotificationManagerは非常に単純で、使用したいときは上記例で示したようにシステムから「getSystemServiceメソッド」を用いて取得します。メソッドも以下の3つしかありません。
- cancel(int id)
指定したIDの通知情報を削除します。ここでいうIDは、notifyメソッドをコールした際に指定したIDです。
- cancelAll()
全ての通知情報を削除します。但し、他のアプリケーションが発行した通知情報には関与できません。
- notify(int id, Notification notification)
通知情報をステータスバーに表示させます。挙動は先に示した通りです。
《Notification》
通知情報のコンテキストクラスです。サウンド、バイブレーション、LEDを用いてアラートする事もできます。
バイブレーションのみ(ステータスバーに通知しない)例
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.vibrate = new long[]{0, 200, 100, 200, 100, 200};
notificationManager.notify(R.string.app_name, notification);
・Manifestにバイブレーションのパーミッション指定をします。
<uses-permission android:name="android.permission.VIBRATE" />
※vibrateに指定する配列は、DELAY_TIME,VIBRATE_TIME、DELAY_TIME・・・の順番です。
また、「通知」ではなく、「実行中」として通知することも可能です。その場合は以下のようにフラグを設定します。このフラグを設定しておくと、「通知を消去」ボタンによって通知情報が消去できなくなります。
notification.flags = Notification.FLAG_ONGOING_EVENT;
一定時間経過すると自動的に通知情報が消去されるようにするには以下のフラグを使います。
notification.flags = Notification.FLAG_AUTO_CANCEL;
taga Android, SDK1.5