Intent intent = new Intent(this,jp.tuyano.sample.MySampleActivity.class);まず、ノーティフィケーションをタッチしたときにアクティビティを起動するためのIntentを用意します。これは、thisと、起動クラスを指定して明示的インテントとして用意しておけばよいでしょう。
PendingIntent pending = PendingIntent.getActivity(this, 0,続いて、Intentを使ってPendingIntentを作成します。これはサービスなどの所で登場したものですが、何かのイベントなどに応じてインテントを実行するのに用いるものです。引数が4つもありますが、最初から「Context(this)」「リクエストコード」「インテント」「フラグ値」を示します。
intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notify = new Notification();ノーティフィケーションの本体であるNotificationインスタンスを作成します。これは引数もなく、簡単ですね。
notify.flags = Notification.FLAG_AUTO_CANCEL;作成したNotificationに必要な設定を行います。ここでは2つのプロパティにそれぞれ値を設定しています。利用しているのは以下のプロパティです。
notify.icon = R.drawable.icon;
notify.tickerText = "Notification!!";
notify.setLatestEventInfo(this, "TEST INFO",「setLatestEventInfo」でステータスバーに表示されるノーティフィケーションのタイトルや表示メッセージ、タッチしたイベント時に実行するPendingIntentなどを設定します。これでNotificationの準備は完了です。
"テストのノーティフィケーションです。", pending);
NotificationManager manager = (NotificationManager)this.後は、Notificationを、サービスに追加するだけです。ノーティケーションはNotificationManagerというサービスで管理されています。これを「getSystemService」で取得します。引数にNOTIFICATION_SERVICEと指定すれば、NotificationManagerが得られます。
getSystemService(Activity.NOTIFICATION_SERVICE);
manager.notify(0, notify);取得したNotificationManagerに、用意したNotificationを登録します。引数には、IDとNotificationインスタンスを指定します。IDは、それぞれで決めてかまいません。
※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
package jp.tuyano.sample; import android.app.*; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.widget.EditText; public class MySampleActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void doAction(View view){ Intent intent = new Intent(this,jp.tuyano.sample.MySampleActivity.class); PendingIntent pending = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); Notification notify = new Notification(); notify.flags = Notification.FLAG_AUTO_CANCEL; notify.icon = R.drawable.icon; notify.setLatestEventInfo(this, "TEST INFO", "テストのノーティフィケーションです。", pending); NotificationManager manager = (NotificationManager)this. getSystemService(Activity.NOTIFICATION_SERVICE); manager.notify(0, notify); } }
<< 前へ | 次へ >> |