2012年2月26日日曜日

[Android SDK] ステータスバーに通知する

ステータスバーを使いユーザに通知する機能があります。実物を見てもらった方が分かりやすいのです。
①ステータスバーに表示する
②ステータスバーに通知が蓄積される

これは、標準で用意されているNotificationManagerで簡単に、任意のメッセージやアイコンを持った通知を発行できます。
まずは、通知内容をNotificationに設定します。
// Context context;
// String message, item_title, item_message;
Notification n = new Notification();
n.icon = R.drawable.ic_launcher;// アイコン(drawable指定)ここではアプリのアイコン
n.tickerText = message;// ステータスバーに表示する文字列(CharSequence指定)
n.when = System.currentTimeMillis();
// 通知をタップしたときに発行するインテント
Intent i = new Intent(context, RpNotifyActivity.class);
n.setLatestEventInfo(context, item_title, item_message, PendingIntent.getActivity(context, 0, i, 0));
あとは、NotificationManagerに渡すとステータスバーに表示されます。
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1,n);

0 件のコメント:

コメントを投稿