C/C++プログラマの管理者が, Androidプログラムにチャレンジ. AndroidプログラミングのTipsをメモっていく予定です.

ステータスバーに情報を通知するには, ノーティフィケーション(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);
  }
}


        アイコンをクリック




コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Menu


逆引き(基礎編)

画面表示/操作(49)

フラグメント(1)

逆引き(応用編)

Firebase(2)

AD



管理人/副管理人のみ編集できます