最終更新: bokkuri_orz 2014年05月31日(土) 20:17:17履歴
- LEDによる通知
参考:http://techbooster.jpn.org/andriod/ui/8843/
- バイブレーションによる通知
参考:http://hiekyo.blog.jp/archives/51146814.html
NotificationManager nmng = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE); Notification.Builder nb = new Notification.Builder(context) .setPriority(Notification.PRIORITY_HIGH) // 高優先の表示 .setContentTitle("バッテリー") // 通知領域に表示されるタイトル .setContentText("battery : " + batteryLevel) // 通知領域に表示されるテキスト .setSmallIcon(R.drawable.icon); // ステータスバー、通知領域に表示されるアイコン nmng.notify(0, nb.build()); // NotificationManagerに登録
※Notification.Builder.build() はAPIレベル16が必要です。
Builderを使わずに、Notificationのメンバに直接設定する方式であれば、
APIレベルを下げることが出来ます。
Intent intent = new Intent(context.getBaseContext(), TargetActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context.getBaseContext(), 0, intent, 0); Notification.Builder nb = new Notification.Builder(sv) .setContentTitle("バッテリー") .setContentText("battery : " + batteryLevel) .setSmallIcon(R.drawable.icon) .setContentIntent(pendingIntent); // タップ時のインテント設定 nmng.notify(0, nb.build()); // NotificationManagerに登録
通知領域、ステータスバーにはアイコンを表示できますが、単一のアイコンではなく、
複数のアイコンを重ねて表示(LayerDrawable)、数値に合わせてアイコンを切り替える(LevelListDrawable)
を利用することも出来ます。
LevelListDrawable, LayerDrawableについては、以下のサイトを参考にして下さい。
作成したDrawableリソースは、setSmallIcon()で使用できます。
確認していないですが、他のDrawableも行けるかも。
Notification.Builder nb = new Notification.Builder(context) .setSmallIcon(R.drawable.res); // R.drawable.res : LevelListDrawable, LayerDrawable を指定する。
こちらのブログの後半に、Drawableリソースを動的に変更する方法が紹介されています。
http://view-onclick.blogspot.jp/2011/10/blog-post....
こんな感じで動的にDrawableを変更できるとか…
// LayerDrawable の Drawable を取得 Resources r = context.getResources(); LayerDrawable layerList = (LayerDrawable)r.getDrawable(R.drawable.layer_list); // LayerDrawableのリソース Drawable drawable = (Drawable)r.getDrawable(R.drawable.iconB); layerList.setDrawableByLayerId(R.id.icon00, drawable); // layerList内の "icon00" と名付けられているアイコンを drawable のものに置き換え layerList.invalidateSelf(); // 更新処理?
ですが、Notificationの通知領域のアイコンは動的に切り替える事ができませんでした。
GL10PMonitorの開発時、バッテリー、受信感度、ネットワーク種類を1つのアイコンにまとめようと試してみましたが、
LayoutDrawableは動作するものの、動的なリソース切り替えは反映されませんでした。
もしかしたら、なにか方法があるのかもしれないですが…
見つかり次第、記事を更新しようと思います。
Notificationのスタイルは標準のもの以外に、次のようなものがあります。
Big Picture Notification | ビットマップ画像の表示 |
Big Text Notification | 複数行のテキストオブジェクトの表示 |
Inbox Style Notification | テキストのリスト、メッセージ、見出しなど |
「ひとりAndroid Advent Calendar」さんの記事に、例を交えてわかりやすく紹介されています。
http://androidadvent.blog.shinobi.jp/2012/android-...
公式ドキュメント
http://developer.android.com/guide/topics/ui/notif...
さらに、RemoteViewsを利用してオリジナルのUIをNotificationに載せることが出来ます。
こちらのサイトにサンプルがあります。
「NotificationのUIをカスタマイズする」
http://android-dev-talk.blogspot.jp/2012/06/notifi...
最新コメント