Android開発爆笑記

レセプターのソフト&ハード開発備忘録

ノーティフィケーション(ステータスバーお知らせ機能)

 public void 関数名( String s ) {
 // NotificationManagerを取得
NotificationManager notificationManager  = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// Notificationを作成 
Notification notification = new Notification(R.drawable.ic_launcher,"ステータスバーに流れるメッセージを書く",System.currentTimeMillis());

// ステータスお知らせのクリックで呼び出すクラスの決定
Intent intent = new Intent(this,MainActivity.class);

// 呼び出すクラスの登録
PendingIntent contentIntent =PendingIntent.getActivity(this, 0, intent, 0);

// notificationに設定
notification.setLatestEventInfo( getApplicationContext() , "アプリ名" , s , contentIntent);

// notificationを実行
notificationManager.notify(R.string.app_name_us, notification);
 }
これだけではアプリ呼出後にステータスに設定されたままになってるので以下で解除する。

onCreate文の中などで記述する。

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(R.string.app_name_us);

※アプリ起動中の呼び出され、2重起動がよくない。