Notification使用詳解
当用户有没有接到的电话的时候,Android顶部状态栏里就会出现一个小图标。提示用户有没有处理的快讯,当拖动状态栏时,可以查看这些快讯。Android给我们提供了NotificationManager来管理这个状态栏。可以很轻松的完成。
? ? 如果要添加一个Notification,可以按照以下几个步骤
1:获取NotificationManager:
NotificationManager m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);2:定义一个Notification:
Notification m_Notification=new Notification();3:设置Notification的各种属性:
m_Notification.icon=R.drawable.icon;? ?? ?? ?? ?? ??
m_Notification.tickerText="Button1通知内容.....";
m_Notification.defaults=Notification.DEFAULT_SOUND;//设置通知显示的参数
Intent m_Intent=new Intent(NotificationDemo.this,DesActivity.class); PendingIntent m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );//这个可以理解为开始执行这个通知
m_NotificationManager.notify(0,m_Notification);4:既然可以增加同样我们也可以删除。当然是只是删除你自己增加的。
m_NotificationManager.cancel(0);??
package com.rocky.studio.ch4221;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.widget.Button;10.import android.widget.TextView;11.public class NotificationDemo extends Activity {12.13.Button m_Button1;14.TextView m_txtView;15.16.NotificationManager m_NotificationManager;17.Notification m_Notification;18.19.Intent m_Intent;20.PendingIntent m_PendingIntent;21. 22. /** Called when the activity is first created. */23. @Override24. public void onCreate(Bundle savedInstanceState) {25. super.onCreate(savedInstanceState);26. setContentView(R.layout.main);27. 28. m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);29. 30. 31. m_Button1=(Button)this.findViewById(R.id.Button01);32. 33. 34. //点击通知时转移内容35. m_Intent=new Intent(NotificationDemo.this,DesActivity.class);36. 37. m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);38. 39. m_Notification=new Notification();40. 41. m_Button1.setOnClickListener(new Button.OnClickListener(){42. public void onClick(View v) {43. // TODO Auto-generated method stub44. 45. //设置通知在状态栏显示的图标46.47. m_Notification.icon=R.drawable.icon;48. 49. //当我们点击通知时显示的内容50. m_Notification.tickerText="Button1 通知内容.....";51. 52. //通知时发出的默认声音53. m_Notification.defaults=Notification.DEFAULT_SOUND;54. 55. //设置通知显示的参数56. m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );57. 58. //这个可以理解为开始执行这个通知59. m_NotificationManager.notify(0,m_Notification);60. 61. }});62. 63. }64. 65. 66.}
package com.rocky.studio.ch4221;2.import android.app.Activity;3.import android.app.NotificationManager;4.import android.os.Bundle;5.public class DesActivity extends Activity {6.7.NotificationManager m_NotificationManager;8.9.@Override10.protected void onCreate(Bundle savedInstanceState) {11. // TODO Auto-generated method stub12. super.onCreate(savedInstanceState); 13. this.setContentView(R.layout.main2);14. 15. //启动后删除之前我们定义的16. m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);17. m_NotificationManager.cancel(0); 18.}19.20.}复制代码
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);Notification代表着一个通知.?
Context context = getApplicationContext(); // Text to display in the extended status window String expandedText = “Extended status text”; // Title for the expanded status String expandedTitle = “Notification Title”; // Intent to launch an activity when the extended text is clicked Intent intent = new Intent(this, MyActivity.class); PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0); notification.setLatestEventInfo(context, expandedTitle, expandedText, launchIntent);? ?
int notificationRef = 1; notificationManager.notify(notificationRef, notification);? ?
notificationManager.cancel(notificationRef);? ?
String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Notification notification = new Notification(); notification.icon = R.drawable.notification_icon;
int icon = R.drawable.notification_icon; //通知图标 CharSequence tickerText = "Hello"; //状态栏(Status Bar)显示的通知文本提示 long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示 Notification notification = new Notification(icon, tickerText, when);
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.tickerText = "hello";
notification.defaults |= Notification.DEFAULT_SOUND; notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
notification.defaults |= Notification.DEFAULT_VIBRATE; long[] vibrate = {0,100,200,300}; notification.vibrate = vibrate;
notification.defaults |= Notification.DEFAULT_LIGHTS; notification.ledARGB = 0xff00ff00; notification.ledOnMS = 300; notification.ledOffMS = 1000; notification.flags |= Notification.FLAG_SHOW_LIGHTS;
private static final int ID_NOTIFICATION = 1; mNotificationManager.notify(ID_NOTIFICATION, notification);