- 论坛徽章:
- 0
|
Android32_Notification用法
Android系统的状态栏(Status Bar)中有一个创新UI设计,这就是可以下拉的通知提示。当系统有一些消息要通知用户时,例如,收到短信、电子邮件、有未接来电时,都会把信息作为通知(Notification)发送给用户。
实例:
Main.xml
Xml代码- 1.<?xml version="1.0" encoding="utf-8"?>
- 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 3. android:orientation="horizontal"
- 4. android:layout_width="fill_parent"
- 5. android:layout_height="fill_parent"
- 6. >
- 7.<Button android:id="@+id/shownotification"
- 8. android:layout_height="wrap_content"
- 9. android:layout_width="wrap_content"
- 10. android:text="显示通知"
- 11./>
- 12.<Button android:id="@+id/deletenotification"
- 13. android:layout_height="wrap_content"
- 14. android:layout_width="wrap_content"
- 15. android:text="清除通知"
- 16./>
- 17.</LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button android:id="@+id/shownotification"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="显示通知"
- />
- <Button android:id="@+id/deletenotification"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="清除通知"
- />
- </LinearLayout>
- NotificationActivity.java
复制代码 Java代码- 1.package com.android.activity;
- 2.import android.app.Activity;
- 3.import android.app.Notification;
- 4.import android.app.NotificationManager;
- 5.import android.app.PendingIntent;
- 6.import android.content.Intent;
- 7.import android.os.Bundle;
- 8.import android.view.View;
- 9.import android.view.View.OnClickListener;
- 10.import android.widget.Button;
- 11.public class NotificationActivity extends Activity {
- 12. private Button showNotification = null;
- 13. private Button deleteNotification = null;
- 14. //Notification管理器
- 15. private NotificationManager nm = null;
- 16. private static final int NOTIFICATION_ID = 123456;
- 17. @Override
- 18. public void onCreate(Bundle savedInstanceState) {
- 19. super.onCreate(savedInstanceState);
- 20. setContentView(R.layout.main);
- 21. //创建 NotificationManager,其中创建的nm 对象负责发出与取消 Notification
- 22. nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- 23. showNotification = (Button)findViewById(R.id.shownotification);
- 24. deleteNotification = (Button)findViewById(R.id.deletenotification);
- 25. showNotification.setOnClickListener(new ShowNotificationListener());
- 26. deleteNotification.setOnClickListener(new DeletedNotificationListener());
- 27. }
- 28. class ShowNotificationListener implements OnClickListener{
- 29. public void onClick(View v) {
- 30. showNotification(R.drawable.image,"图标边的文字","标题","内容");
- 31. }
- 32. }
- 33. class DeletedNotificationListener implements OnClickListener{
- 34. public void onClick(View v) {
- 35. //表示当用户点击 Clear 之后,能够清除该通知。
- 36. nm.cancel(NOTIFICATION_ID);
- 37. }
- 38. }
- 39. public void showNotification(int icon,String tickertext,
- 40. String title,String content){
- 41. //创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。
- 42. Notification notification = new Notification(icon,tickertext,System.currentTimeMillis());
- 43. //这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND
- 44. //振动为Notification.DEFAULT_VIBRATE;
- 45. //Light为Notification.DEFAULT_LIGHTS
- 46. //全部为Notification.DEFAULT_ALL
- 47. //如果是振动或者全部,必须在AndroidManifest.xml加入振动权限
- 48. notification.defaults = Notification.DEFAULT_ALL;
- 49. //创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent
- 50. //请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
- 51. //Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例
- 52. //Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity
- 53. Intent intent = new Intent(this,NotificationActivity.class);
- 54. //PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0),用于系统发送的Intent,标志位
- 55. PendingIntent pt = PendingIntent.getActivity(this, 0,intent, 0);
- 56. notification.setLatestEventInfo(this,title,content,pt);
- 57. //启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification
- 58. nm.notify(NOTIFICATION_ID, notification);
- 59. }
- 60.}
- package com.android.activity;
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class NotificationActivity extends Activity {
- private Button showNotification = null;
- private Button deleteNotification = null;
- //Notification管理器
- private NotificationManager nm = null;
- private static final int NOTIFICATION_ID = 123456;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //创建 NotificationManager,其中创建的nm 对象负责发出与取消 Notification
- nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- showNotification = (Button)findViewById(R.id.shownotification);
- deleteNotification = (Button)findViewById(R.id.deletenotification);
- showNotification.setOnClickListener(new ShowNotificationListener());
- deleteNotification.setOnClickListener(new DeletedNotificationListener());
- }
- class ShowNotificationListener implements OnClickListener{
- public void onClick(View v) {
- showNotification(R.drawable.image,"图标边的文字","标题","内容");
- }
- }
- class DeletedNotificationListener implements OnClickListener{
- public void onClick(View v) {
- //表示当用户点击 Clear 之后,能够清除该通知。
- nm.cancel(NOTIFICATION_ID);
- }
- }
- public void showNotification(int icon,String tickertext,
- String title,String content){
- //创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。
- Notification notification = new Notification(icon,tickertext,System.currentTimeMillis());
- //这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND
- //振动为Notification.DEFAULT_VIBRATE;
- //Light为Notification.DEFAULT_LIGHTS
- //全部为Notification.DEFAULT_ALL
- //如果是振动或者全部,必须在AndroidManifest.xml加入振动权限
- notification.defaults = Notification.DEFAULT_ALL;
- //创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent
- //请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
- //Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例
- //Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity
- Intent intent = new Intent(this,NotificationActivity.class);
- //PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0),用于系统发送的Intent,标志位
- PendingIntent pt = PendingIntent.getActivity(this, 0,intent, 0);
- notification.setLatestEventInfo(this,title,content,pt);
- //启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification
- nm.notify(NOTIFICATION_ID, notification);
- }
- }
复制代码 运行结果:
文字消失后:
清除通知后:
|
|