忍者ブログ

アンドロイドのあれこれ

[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

Android 4.1 Jelly Bean Notification Styles
Android 4.1 Jelly Bean、API level 16から新しい通知のスタイルが3つ追加されました。それぞれのサンプルを紹介します。

Notifications | Android Developers
http://developer.android.com/guide/topics/ui/notifiers/notifications.html


Basic Notification – アイコンの付いたシンプルな通知の表示
Big Picture Notification – 画像のビットマップなどの視覚的なコンテンツの表示
Big Text Notification – 複数行のテキストオブジェクトの表示
Inbox Style Notification – テキストのリスト、メッセージ、見出しなど

Notificationクラスの宣言は Notification.Builder を使います。
Notification(int icon, CharSequence tickerText, long when)ではAPI level 11では廃止されている予定なので推薦されていません。

まず、基本なスタイルのNotification
...
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification.Builder(this)
.setContentTitle("Basic Notification")
.setContentText("Basic Notification の サンプル")
.setSmallIcon(android.R.drawable.ic_menu_info_details)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
showNotification(notification);

notificationManager.notify(0, notification);
...
実行イメージ
Android 4.1 Notification Styles


Notification.BigPictureStyle
...
PendingIntent showActivityIntent
= PendingIntent.getActivity(this, 0, getIntent(), 0);
PendingIntent shareIntent
= PendingIntent.getActivity(this, 0, getIntent(), 0);

Builder builder = new Notification.Builder(this);
builder.setContentTitle("BigPicture Notification")
.setContentText("BigPicutre Notification のサンプル")
.setSmallIcon(android.R.drawable.ic_menu_info_details)
.addAction(android.R.drawable.ic_media_play, "Show", showActivityIntent)
.addAction(android.R.drawable.ic_menu_share, "Share", shareIntent);

Notification notification = new Notification.BigPictureStyle(builder)
.bigPicture(
BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher))
.build();

notification.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, notification);
....
実行イメージ
Android 4.1 Jelly Bean Notification.BigPictureStyle


Notification.BigTextStyle
...
String msgText = "Jeally Bean Notification サンプル"
+ "ここに非常に長い文字列を入れることができますここに非常に長い文字列を入れることができます。 "
+ "ここに非常に長い文字列を入れることができます。";
PendingIntent showIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);
Builder builder = new Notification.Builder(this);
builder.setContentTitle("BigText Notofication")
.setContentText("BigText Notification のサンプル")
.setSmallIcon(android.R.drawable.ic_menu_info_details)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(android.R.drawable.ic_media_play, "show activity", showIntent);
Notification notification = new Notification.BigTextStyle(builder)
.bigText(msgText).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, notification);
...
実行イメージ
Android 4.1 Jelly Bean Notification.BigTextStyle


Notification.InboxStyle
...
PendingIntent showIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);
Builder builder = new Notification.Builder(this)
.setContentTitle("Inbox Notification")
.setContentText("Inbox Style Notification のサンプル")
.setSmallIcon(android.R.drawable.ic_menu_info_details)
.addAction(android.R.drawable.ic_media_play, "show activity", showIntent);

Notification notification = new Notification.InboxStyle(builder)
.addLine("This is message 1")
.addLine("This is message 2")
.addLine("This is message 3")
.addLine("This is message 4")
.setSummaryText("+2 more").build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, notification);
...
実行イメージ
Android 4.1 Jelly Bean Notification.InboxStyle
COMMENT
NAME
TITLE
MAIL (非公開)
URL
EMOJI
Vodafone絵文字 i-mode絵文字 Ezweb絵文字
COMMENT
PASS (コメント編集に必須です)
SECRET
管理人のみ閲覧できます
 
PR
© Android Advent
powered by 忍者ツールズ / 忍者ブログ / [PR]