最終更新:
moonlight_aska 2012年04月15日(日) 01:45:54履歴
ステータスバーに情報を通知するには, ノーティフィケーション(Notification)クラスとノーティフィケーションマネージャ(NotificationManager)クラスを使用する.

↓ アイコンをクリック

- Notification01.java
- PendingIntent#getActivityメソッドで, クリック時のPendingIntentを生成する.
- getSystemServiceメソッドでNotificationManagerのインスタンスを取得する.
- Notificaitonのインスタンスを生成する.
- ステータスバーに表示するアイコンや文字列を設定する.
- Notification#setLatestEventInfoメソッドで, 通知する情報を設定する.
- NotificationManager#notifyメソッドで, 通知する.
package com.moonlight_aska.android.notification01;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class Notification01 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// PendingIntentの生成
Intent intent = new Intent(Intent.ACTION_VIEW);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
// NotificationManagerのインスタンスを取得
NotificationManager notifManager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Notificationのインスタンスの生成
Notification notif = new Notification();
notif.icon = R.drawable.ic_launcher;
notif.tickerText = "通知だよ!! 見てね.";
notif.setLatestEventInfo(getApplicationContext(), "タイトル:App名",
"メッセージ:これは通知情報ですよ。", pendIntent);
// Notificationの通知
notifManager.notify(R.string.app_name, notif);
}
}

↓ アイコンをクリック

コメントをかく