- A Toast Notification, 从屏幕弹出剪短的消息。
- A Status Notification, 标准的通知。
- A Dialog Notification, 对话框式的通知。
Toast Notification
Toast通知是一条弹出显示在窗口表面的消息,它只占据足够显示消息内容的屏幕空间,并且用户当前的activity仍然保持可见和可操作。这个通知自动淡入淡出,并不接收交互事件。当一个闹铃开启的时候,一条toast消息显示出来提醒你设置闹铃成功。Toast是在屏幕表面显示片刻的一条消息,它不会抢占用户焦点(或者暂停当前的activity),所以它不接收用户输入,你可以自定义Toast的布局layout,使其包含图片。
可以从Activity或者Service创建并显示一条toast消息。如果你从Service创建一条toast消息,它会显示在当前焦点的activity之上。
如果需要用户回应一个通知,可以考虑使用状态栏通知。
基础
首先,用makeText()方法实例化一个Toast对象。该方法需要三个参数:当前应用的Context,文本消息,和toast的持续时间。该方法返回一个实例化过的Toast对象。你可以用show()方法将该toast通知显示出来,见下例:
Context context = getApplicationContext();
CharSequence text = "Hello toast!"
intduration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
这个例子演示大部分你使用toast通知所需要的,你很少会需要其他的。有可能你会想要把toast放置在其他位置或者使用你自己的布局来代替默认的简单文本消息布局。下一节描述了如何使用它们。
你也可以链接调用方法已避免保留一个Toast对象,例如:
Toast.makeText(context, text, duration).show();
定位你的toast
一个标准toast通知垂直剧中出现在靠近屏幕底部的位置。你可以通过setGravity(int, int, int)方法来改变其位置。三个参数分别是:一个Gravity常量,一个x方向的偏移值和一个y方向的偏移值。
例如,如果你决定让toast出现在左上角,你可以这样设置:
toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
如果你想要向右移动,增加第二个参数的值;增加第三个参数的值向下移动。
创建自定义的Toast视图
例如,你可以用如下所示XML创建右图中的toast通知的布局(保存为toast_layout.xml):
如果一个简单的文本消息已经无法满足你的需求,你可以自己定义一个toast通知的布局layout。在XML或者代码中定义一个View的布局,并根View对象传递给setView(View)方法。
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#DAAA" >
<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:layout_textColor="#FFF"/>
</LinearLayout>
注意LinearLayout元素的ID是“toast_layout"。你必须使用这个ID从XML中
展开(inflate)
布局layout,如下所示:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
首先,通过getLayoutInflater()(或者getSystemService())取得LayoutInflater,然后用inflate(int, ViewGroup)从XML展开(inflate)布局layout。第一个参数是layout资源ID,第二个参数是根View。你可以使用这个布局来查找其内部更多的View对象。因此获得和定义ImageView和TextView元素的内容。最后,用Toast(Context)创建一个新的toast并设置属性,例如重心和持续时间。然后调用setView(View)并传入布局对象,你就可以通过show()来显示自定义布局的toast了。
注意: 不要使用Toast的公共构造函数,除非你要用setView(View)来定义布局。如果你没有自定义布局,你必须使用makeText(Context, int, int)来创建toast。
标准通知:
创建一个标准的通知:
- 获取到NotificationManager:
String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
- 实例化notification:
int icon = R.drawable.notification_icon; CharSequence tickerText = "Hello"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when);
- 定义通知的 message and PendingIntent:
Context context = getApplicationContext(); CharSequence contentTitle = "My notification"; CharSequence contentText = "Hello World!"; Intent notificationIntent = new Intent(this, MyClass.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
- 将notification放到NotificationManager里面
private static final int HELLO_ID = 1; mNotificationManager.notify(HELLO_ID, notification);
通知的响应:
-
通知的典型例子是日历,它可以发送即将到来的事件的通知,和电子邮件,它可以发送通知时,新的消息到达。这些表示处理通知有两个推荐模式:要么启动到一个单独的活动中,要么发布一个完全新的应用程序的实例,显示通知的适当点。
-
/** * This method creates an array of Intent objects representing the * activity stack for the incoming message details state that the * application should be in when launching it from a notification. */ static Intent[] makeMessageIntentStack(Context context, CharSequence from, CharSequence msg) { // A typical convention for notifications is to launch the user deeply // into an application representing the data in the notification; to // accomplish this, we can build an array of intents to insert the back // stack stack history above the item being displayed. Intent[] intents = new Intent[4]; // First: root activity of ApiDemos. // This is a convenient way to make the proper Intent to launch and // reset an application's task. intents[0] = Intent.makeRestartActivityTask(new ComponentName(context, com.example.android.apis.ApiDemos.class)); // "App" intents[1] = new Intent(context, com.example.android.apis.ApiDemos.class); intents[1].putExtra("com.example.android.apis.Path", "App"); // "App/Notification" intents[2] = new Intent(context, com.example.android.apis.ApiDemos.class); intents[2].putExtra("com.example.android.apis.Path", "App/Notification"); // Now the activity to display to the user. Also fill in the data it // should display. intents[3] = new Intent(context, IncomingMessageView.class); intents[3].putExtra(IncomingMessageView.KEY_FROM, from); intents[3].putExtra(IncomingMessageView.KEY_MESSAGE, msg); return intents; } /** * The notification is the icon and associated expanded entry in the * status bar. */ void showAppNotification() { // look up the notification manager service NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // The details of our fake message CharSequence from = "Joe"; CharSequence message; switch ((new Random().nextInt()) % 3) { case 0: message = "r u hungry? i am starved"; break; case 1: message = "im nearby u"; break; default: message = "kthx. meet u for dinner. cul8r"; break; } // The PendingIntent to launch our activity if the user selects this // notification. Note the use of FLAG_CANCEL_CURRENT so that, if there // is already an active matching pending intent, cancel it and replace // it with the new array of Intents. PendingIntent contentIntent = PendingIntent.getActivities(this, 0, makeMessageIntentStack(this, from, message), PendingIntent.FLAG_CANCEL_CURRENT); // The ticker text, this uses a formatted string so our message could be localized String tickerText = getString(R.string.imcoming_message_ticker_text, message); // construct the Notification object. Notification notif = new Notification(R.drawable.stat_sample, tickerText, System.currentTimeMillis()); // Set the info for the views that show in the notification panel. notif.setLatestEventInfo(this, from, message, contentIntent); // We'll have this notification do the default sound, vibration, and led. // Note that if you want any of these behaviors, you should always have // a preference for the user to turn them off. notif.defaults = Notification.DEFAULT_ALL; // Note that we use R.layout.incoming_message_panel as the ID for // the notification. It could be any integer you want, but we use // the convention of using a resource id for a string related to // the notification. It will always be a unique number within your // application. nm.notify(R.string.imcoming_message_ticker_text, notif); }
<activity android:name=".app.IncomingMessageInterstitial" android:label="You have messages" android:theme="@style/ThemeHoloDialog" android:launchMode="singleTask" android:taskAffinity="" android:excludeFromRecents="true"> </activity>
/** * Perform a switch to the app. A new activity stack is started, replacing * whatever is currently running, and this activity is finished. */ void switchToApp() { // We will launch the app showing what the user picked. In this simple // example, it is just what the notification gave us. CharSequence from = getIntent().getCharSequenceExtra(IncomingMessageView.KEY_FROM); CharSequence msg = getIntent().getCharSequenceExtra(IncomingMessageView.KEY_MESSAGE); // Build the new activity stack, launch it, and finish this UI. Intent[] stack = IncomingMessage.makeMessageIntentStack(this, from, msg); startActivities(stack); finish(); }
创建一个通知: -
int icon = R.drawable.notification_icon; // icon from resources CharSequence tickerText = "Hello"; // ticker-text long when = System.currentTimeMillis(); // notification time Context context = getApplicationContext(); // application Context CharSequence contentTitle = "My notification"; // message title CharSequence contentText = "Hello World!"; // message text Intent notificationIntent = new Intent(this, MyClass.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); // the next two lines initialize the Notification, using the configurations above Notification notification = new Notification(icon, tickerText, when); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
Updating the notification
更新通知: -
- // 用来更新通知栏消息的handler
- private Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MSG:
- int newMsgNum = application.getNewMsgNum();// 从全局变量中获取
- newMsgNum++;// 每收到一次消息,自增一次
- application.setNewMsgNum(newMsgNum);// 再设置为全局变量
- TranObject<TextMessage> textObject = (TranObject<TextMessage>) msg
- .getData().getSerializable("msg");
- // System.out.println(textObject);
- if (textObject != null) {
- int form = textObject.getFromUser();// 消息从哪里来
- String content = textObject.getObject().getMessage();// 消息内容
- ChatMsgEntity entity = new ChatMsgEntity("",
- MyDate.getDateEN(), content, -1, true);// 收到的消息
- messageDB.saveMsg(form, entity);// 保存到数据库
- // 更新通知栏
- int icon = R.drawable.notify_newmessage;
- CharSequence tickerText = form + ":" + content;
- long when = System.currentTimeMillis();
- mNotification = new Notification(icon, tickerText, when);
- mNotification.flags = Notification.FLAG_NO_CLEAR;
- // 设置默认声音
- mNotification.defaults |= Notification.DEFAULT_SOUND;
- // 设定震动(需加VIBRATE权限)
- mNotification.defaults |= Notification.DEFAULT_VIBRATE;
- mNotification.contentView = null;
- Intent intent = new Intent(mContext,
- FriendListActivity.class);
- PendingIntent contentIntent = PendingIntent.getActivity(
- mContext, 0, intent, 0);
- mNotification.setLatestEventInfo(mContext, util.getName()
- + " (" + newMsgNum + "条新消息)", content,
- contentIntent);
- }
- mNotificationManager.notify(Constants.NOTIFY_ID, mNotification);// 通知一下才会生效哦
- break;
- default:
- break;
- }
- }
- };
Adding a sound
你可以用默认的通知的声音来提醒用户.
notification.defaults |= Notification.DEFAULT_SOUND;
To use a different sound with your notifications, pass a Uri reference to the sound field. The following example uses a known audio file saved to the device SD card:
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
In the next example, the audio file is chosen from the internal MediaStore's ContentProvider:
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
Adding flashing lights
闪光灯来提醒用户:
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00; notification.ledOnMS = 300; notification.ledOffMS = 1000; notification.flags |= Notification.FLAG_SHOW_LIGHTS;自定义通知:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_marginRight="10dp" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/image" style="@style/NotificationTitle" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/image" android:layout_below="@id/title" style="@style/NotificationText" /> </RelativeLayout>
For example, to use the standard text colors on versions of Android lower than 2.3, you should use the following styles for res/values/styles.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="NotificationText"> <item name="android:textColor">?android:attr/textColorPrimary</item> </style> <style name="NotificationTitle"> <item name="android:textColor">?android:attr/textColorPrimary</item> <item name="android:textStyle">bold</item> </style> <!-- If you want a slightly different color for some text, consider using ?android:attr/textColorSecondary --> </resources>
Then, to apply the system's default colors for notifications on Android 2.3 and higher, use the following styles for res/values-v9/styles.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" /> <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" /> </resources>
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout); contentView.setImageViewResource(R.id.image, R.drawable.notification_image); contentView.setTextViewText(R.id.title, "Custom notification"); contentView.setTextViewText(R.id.text, "This is a custom layout"); notification.contentView = contentView;
Intent notificationIntent = new Intent(this, MyClass.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.contentIntent = contentIntent;
mNotificationManager.notify(CUSTOM_VIEW_ID, notification);进度条通知:
小窗口显示的时候用,在dialog里面有这个内容.