Notificatin
[功能]
左上角的那个信息提示 可以弹出一些信息 比如 状态更新 或 其他
[img]http://dl.iteye.com/upload/picture/pic/52746/118cbfed-14b7-3da8-a4e4-3115fb11e9ff.png[/img]
[思路]
1. 得到NotificationManager 用于把Notification抛出
2. 构造一个Notification 设定与Notification相关的信息 包括2个方面:左上角提示信息 和 Expanded 信息
[实现]
1. 得到NotificationManager的实例 用于抛出Notification
2. 构造一个Notification 并传入一些信息 比如 图标 文字 弹出时间
3. 设定Notification的expand 信息 可通过 HOMR->MENU->Notification 查看
[img]http://dl.iteye.com/upload/picture/pic/52748/ce3a5804-f120-393d-86f0-40489b75b971.png[/img]
4. 抛出Notification.
5. 定制Expand 信息也是可以的
× 定制目标的 custom_notification_layout.xml
× 设定一些使用信息 比如 文章内容
其他信息稍后补充 比如 Notification弹出的地方 Expand地方!
[功能]
左上角的那个信息提示 可以弹出一些信息 比如 状态更新 或 其他
[img]http://dl.iteye.com/upload/picture/pic/52746/118cbfed-14b7-3da8-a4e4-3115fb11e9ff.png[/img]
[思路]
1. 得到NotificationManager 用于把Notification抛出
2. 构造一个Notification 设定与Notification相关的信息 包括2个方面:左上角提示信息 和 Expanded 信息
[实现]
1. 得到NotificationManager的实例 用于抛出Notification
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2. 构造一个Notification 并传入一些信息 比如 图标 文字 弹出时间
int icon = R.drawable.icon;
CharSequence tickerText = "HelloNotfication";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);;
notification.defaults=Notification.DEFAULT_SOUND;
3. 设定Notification的expand 信息 可通过 HOMR->MENU->Notification 查看
[img]http://dl.iteye.com/upload/picture/pic/52748/ce3a5804-f120-393d-86f0-40489b75b971.png[/img]
//to definition some vale in Expanded view
CharSequence contentTitle = "Notification's title @ Expanded view"; // expanded message title
CharSequence contentText = "Notification's text @ Expanded view"; // expanded message text
//to definition an Intent that this Notification listener in Expanded view via clicked action
Intent notificationIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel://110"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
4. 抛出Notification.
manager.notify(1, notification);
5. 定制Expand 信息也是可以的
× 定制目标的 custom_notification_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000"
/>
</LinearLayout>
× 设定一些使用信息 比如 文章内容
// to customize its Expanded view via XML file
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.msn);
// to specific the text in Expanded view
contentView.setTextViewText(R.id.text, "Hello, this is Expanded view with custom XML");
notification.contentView = contentView;
其他信息稍后补充 比如 Notification弹出的地方 Expand地方!