ノーティフィケーションの利用 (5/5)
作成:2011-10-10 10:41
更新:2011-10-10 10:43
更新:2011-10-10 10:43
■オリジナルの表示を作成する
ノーティフィケーションの表示は、「RemoteView」と呼ばれるビューとして用意されます。これを独自に定義して設定してやれば、実は表示をまるっきり変えてしまうことも可能です。このRemoteViewは、以下のようにして作成をします。
こうして作成したRemoteViewを、Notificationのコンテントビューとして設定してやれば、表示を変更することが可能です。
RemoteViews remoteview = new第1引数にはパッケージ名を指定します。これは、このActivityクラスのgetPackageNameメソッドを呼び出し取得すればいいでしょうそして第2引数にはレイアウト用のリソースIDを指定します。あらかじめ、ノーティフィケーションで表示するレイアウトをXMLファイルとして用意しておき、ここにそのIDを設定します。
RemoteViews(パッケージ名, リソースID );
こうして作成したRemoteViewを、Notificationのコンテントビューとして設定してやれば、表示を変更することが可能です。
【Notification】.contentView =【RemoteView】;ただし、これだけではうまくいきません。RemoteViewを独自に追加する場合、Notificationの「setLatestEventInfo」は使えなくなります。したがって、PendingIntentを別のやり方で設定してやる必要があるのです。
【Notification】.contentIntent =【PendingIntent】;これでOKです。下のリスト欄に独自の表示を行うノーティフィケーションの例を挙げておきました。ここでは、item.xmlというファイルを作成してレイアウトを用意し、これを利用することにしています。実際にitem.xmlにレイアウトを配置して試してみましょう。オリジナルの表示ができるというのは、なかなか面白いですよ!
(by. SYODA-Tuyano.)
※プログラムリストが表示されない場合
AddBlockなどの広告ブロックツールがONになっていると、プログラムリスト等が表示されない場合があります。これらのツールをOFFにしてみてください。
●プログラム・リスト●
※item.xmlのソースコード <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:layout_weight="0.03" android:layout_height="fill_parent" android:layout_width="fill_parent" android:text="サンプルで作ったView" android:background="#66CC00"></TextView> </LinearLayout> ※オリジナルの表示を行う public void doAction(View view){ RemoteViews remoteview = new RemoteViews(this.getPackageName(), R.layout.item); 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.contentView = remoteview; notify.flags = Notification.FLAG_AUTO_CANCEL; notify.icon = R.drawable.icon; notify.contentIntent = pending; NotificationManager manager = (NotificationManager)this. getSystemService(Activity.NOTIFICATION_SERVICE); manager.notify(0, notify); }
※関連コンテンツ