您现在的位置:首页 >> android图书管理系统 >> android 通知栏,android内存管理机制,android 自定义通知栏,Android通知管理

android 通知栏,android内存管理机制,android 自定义通知栏,Android通知管理

时间:2013-08-19 来源: 泥巴往事网

android通知管理 android中notification的应用. 资源积分: 2分 下载次数: 6 资源类型: 代码类 资源大小: 987KB 资源得分: (0位用户参与评分) 举报 若举报审核通过,可奖励...

Android 通知管理(NotificationManager)的使用,包括 震动,led 闪屏 在 android 开发中经常要使用到通知,比如:收到短息,来电等等,通知是应用 程序提醒用户的一种方式,他不需要使用 Activity。

通知向用户传递信息有多种方式:(1)状态栏图标 (2)扩展的通知状态绘制 器 (3)声音、震动、LED 闪烁 通过一个小例子将上面几种方式集成到一起。

在配置扩展状态通知显示的时候,有两种方法:

1, 使用 setLatestEventInfo 方法更新标准的扩展的状态通知显示中所显示的 详细信息。

2,使用远程视图(Remote View)设置 contentView 和 contentIntent,这样 可以为扩展状态显示分配一个你需要的定制 UI. 扩展状态窗口定制布局 my_status.xml(一个简单线性布局,前面放一个 imageview,后面一个 textview) [html] view plaincopy 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. <?xml version="1.0"

encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal">

<ImageView android:id="@+id/status_icon"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

<TextView android:id="@+id/status_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

</LinearLayout> 要把这个定制布局分配给通知,要创建一个新的 RemoteView 对象,并把它分配 给 contentView 属性,还需要想 contentIntent 属性分配一个待处理的意图 (Pending Intent)代码如下: [java] view plaincopy 1. 2. mNotification.contentView = new RemoteViews(this.getPackageName(),R.layout.m y_status);

mNotification.contentIntent = mContentIntent; 如果要修改定制布局中视图的属性或者外观,可以使用远程视图对象的 set*方 法 [java] view plaincopy 1. con); mNotification.contentView.setImageViewResource(R.id.status_icon,R.drawable.i [java] view plaincopy 1. mNotification.contentView.setTextViewText(R.id.status_text, "This is test co ntent"); 向通知添加声音、 闪屏、 振动效果的最简单最一致的方式是使用当前用户的默认 设置。 [java] view plaincopy 1. IBRATE; mNotification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_V 如果想全部默认设置就使用 DEFAULT_ALL 常量 如果要使用自己的定制的声音或者动态修改声音就可以设置 mNotification.sound = ringURI;

要设置振动类型的话,需要向通知的 vibrate 属性分配一个 longs 类型的数组; 比如: [java] view plaincopy 1. 2. long[] vibreate= new long[]{1000,1000,1000,1000,1000};

mNotification.vibrate = vibreate; 上面代码的作用是振动按照振动 1 秒,暂停 1 秒的模式进行振动,整个过程持续 5 秒。

需要注意的是,使用振动必须添加一个权限:

1. <uses-permission android:name="android.permission.VIBRATE"/> 设置闪屏 mNotification.ledARGB = Color.BLUE;

mNotification.ledOffMS= 0;

mNotification.ledOnMS = 1;

mNotification.flags = mNotification.flags | Notification.FLAG_SHOW_LIGHTS;

ledARGB 属性可以用来设置 LED 的颜色,ledOffMS 和 ledOnMS 属性则可以设置 LED 闪烁的频率和模式。ledOnMS 设置为 1 并把 ledOffMS 设置为 0 来打开 LED, 两个都设置为 0 则关闭 LED. 完整的代码:

主界面布局文件 1. 2. 3. <?xml version="1.0"

encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="通知测试小程序"

/>

<Button android:id="@+id/showStatusButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="测试通知"

/>

<Button android:id="@+id/cancelButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="取消通知"

/>

</LinearLayout> activity 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.graphics.Color;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RemoteViews;

public class NotificationDemo extends Activity implements OnClickListener{ private Context mContext;

private Button showStatusButton,cancelButton;

private Notification mNotification;

private NotificationManager mNotificationManager;

private final static int NOTIFICATION_ID = 0x0001; 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 报错. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mNotification = new Notification(R.drawable.icon,"This is a notifica tion.",System.currentTimeMillis());

mNotificationManager = (NotificationManager)this.getSystemService(NO TIFICATION_SERVICE); findViews();

} public void findViews(){ mContext = NotificationDemo.this;

showStatusButton = (Button)findViewById(R.id.showStatusButton);

cancelButton = (Button)findViewById(R.id.cancelButton); showStatusButton.setOnClickListener(this);

cancelButton.setOnClickListener(this);

} public void statusNotifi(){ Intent mIntent = new Intent(mContext,NotificationDemo.class);

//这里需要设置 Intent.FLAG_ACTIVITY_NEW_TASK 属性 mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent mContentIntent =PendingIntent.getActivity(mContext,0, mIntent, 0);

//1,使用 setLatestEventInfo //这里必需要用 setLatestEventInfo(上下文,标题,内容,PendingIntent)不然会 // mNotification.setLatestEventInfo(mContext, "新消息", "主人,您孙子 48. 49. 50. 51. 52. 53. awable.icon); 给你来短息了", mContentIntent); //2,使用远程视图 mNotification.contentView = new RemoteViews(this.getPackageName(),R. layout.my_status);

mNotification.contentView.setImageViewResource(R.id.status_icon,R.dr mNotification.contentView.setTextViewText(R.id.status_text, "This is test content"); 54. 55. 56. //使用默认的声音,闪屏,振动效果 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. IGHTS; // // EFAULT_VIBRATE; mNotification.defaults = Notification.DEFAULT_ALL;

mNotification.defaults = Notification.DEFAULT_SOUND | Notification.D //添加震动 long[] vibreate= new long[]{1000,1000,1000,1000};

mNotification.vibrate = vibreate;

//添加 led mNotification.ledARGB = Color.BLUE;

mNotification.ledOffMS= 0;

mNotification.ledOnMS = 1;

mNotification.flags = mNotification.flags | Notification.FLAG_SHOW_L 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. //手动设置 contentView 属于时,必须同时也设置 contentIntent 不然会报错 mNotification.contentIntent = mContentIntent;

//触发通知(消息 ID,通知对象) mNotificationManager.notify(NOTIFICATION_ID, mNotification); } public void onClick(View v) { if(v == showStatusButton){ statusNotifi();

}else if(v == cancelButton){ mNotificationManager.cancel(NOTIFICATION_ID);

} } } //取消通知 提示:

每个设备对 LED 的控制方面可能具有不同的限制, 如果设置的颜色不可用, 可以尝试换用其他颜色试试。

振动和 LED 在模拟器中是看不到效果的,必须使用真机。 如何在 Android 中设置铃声+震动 有时候一些通讯软件需要这些个功能,比如说收到短信,通知等,要求手机发出铃声,或震 动,或发光以提示用户知晓。往往手机都是有默认设置的,比如说用户开启了铃声+震动; 只铃声不震动;完全静音等等... 这个时候就需要有一个规则了, 起码软件的设置不能跟系统的冲突吧, 中间的一些逻辑是要 处理好的!之前做过的软件中有这么个需求,而且代码是我负责的,所以总结一下。

思路:

1. 软件应该有个自己的设置配置文件,用以保存,自己的软件的提醒规则 2. 遵从系统的设置,比如说:系统是完全静音的,人家想睡觉啦,你软件虽然是铃声 震动全开,也得乖乖闭嘴。

3. 如果有需要提醒了,先获取系统的配置,然后做逻辑判断给予什么样的提醒。

代码: 1 //首先需要接收一个 Notification 的参数 2 private void setAlarmParams(Notification notification) { 3 //AudioManager provides access to volume and ringer mode 4 control. 5 AudioManager volMgr = (AudioManager) 6 mAppContext.getSystemService(Context.AUDIO_SERVICE);

7 switch (volMgr.getRingerMode()) {//获取系统设置的铃声模 8式 9 case AudioManager.RINGER_MODE_SILENT://静音模式,值为 1 0,这时候不震动,不响铃 0 notification.sound = null;

1 notification.vibrate = null;

1 break;

1 case AudioManager.RINGER_MODE_VIBRATE://震动模式, 值为 2 1,这时候震动,不响铃 1 notification.sound = null;

3 notification.defaults |= 1 Notification.DEFAULT_VIBRATE;

4 break;

1 case AudioManager.RINGER_MODE_NORMAL://常规模式,值为 5 2,分两种情况:1_响铃但不震动,2_响铃+震动 1 Uri ringTone = null;

6 //获取软件的设置 1 SharedPreferences sp = 7 PreferenceManager.getDefaultSharedPreferences(mAppContext);

1 if(!sp.contains(SystemUtil.KEY_RING_TONE)){//如果 8 没有生成配置文件,那么既有铃声又有震动 1 notification.defaults |= 9 Notification.DEFAULT_VIBRATE;

2 notification.defaults |= 0 Notification.DEFAULT_SOUND;

2 }else{ 1 String ringFile = 2 sp.getString(SystemUtil.KEY_RING_TONE, null);

2 if(ringFile==null){//无值,为空,不播放铃声 2 ringTone=null;

3 }else if(!TextUtils.isEmpty(ringFile)){//有铃 2 声:1,默认 2 自定义,都返回一个 uri 4 ringTone=Uri.parse(ringFile);

2 } 5 notification.sound = ringTone;

2 6 boolean vibrate = 2 sp.getBoolean(SystemUtil.KEY_NEW_MAIL_VIBRATE,true);

7 if(vibrate == false){//如果软件设置不震动,那 2 么就不震动了 8 notification.vibrate = null;

2 }else{//否则就是需要震动, 这时候要看系统是怎么 9 设置的:不震动=0;震动=1;仅在静音模式下震动=2; 3 if(volMgr.getVibrateSetting(AudioManager. 0 VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_OFF){ 3 //不震动 1 notification.vibrate = null;

3 }else if(volMgr.getVibrateSetting(AudioMa 2 nager.VIBRATE_TYPE_RINGER) == 3 AudioManager.VIBRATE_SETTING_ONLY_SILENT){ 3 //只在静音时震动 3 notification.vibrate = null;

4 }else{ 3 //震动 5 notification.defaults |= 3 Notification.DEFAULT_VIBRATE;

6 } 3 } 7 } 3 notification.flags |= 8 Notification.FLAG_SHOW_LIGHTS;//都给开灯 3 break;

9 default: 4 break;

0 } 4 } 1 4 2 4 3 4 具体的实现就如代码那样子了,注释也很清楚了,其中 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mAppContext); 【Android】状态栏通知 Notification、 NotificationManager 详解 在 Android 系统中,发一个状态栏通知还是很方便的。下面我们就来看一下,怎么发送状态 栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类:

NotificationManager 、 Notification。

NotificationManager :

是状态栏通知的管理类,负责发通知、清楚通知等。

NotificationManager 是一个系统 Service,必须通过 getSystemService()方法来获取。 [java] view plaincopy 1. NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION _SERVICE); Notification:是具体的状态栏通知对象,可以设置 icon、文字、提示声音、振动等等参数。

下面是设置一个通知需要的基本参数: ? ? ? An icon (通知的图标) A title and expanded message (通知的标题和内容) A PendingIntent (点击通知执行页面跳转) 可选的设置: ? ? ? ? ? A ticker-text message (状态栏顶部提示消息) An alert sound (提示音) A vibrate setting (振动) A flashing LED setting (灯光) 等等 一、创建 Notification 通过 NotificationManager 的 notify(int, Notification) 方法来启动 Notification。 第一个参数唯一的标识该 Notification,第二个参数就是 Notification 对象。

二、更新 Notification 调用 Notification 的 setLatestEventInfo 方法来更新内容,然后再调用 NotificationManager 的 notify()方法即可。(具体可以看下面的实例) 三、删除 Notification 通过 NotificationManager 的 cancel(int)方法, 来清除某个通知。

其中参数就是 Notification 的唯一标识 ID。

当然也可以通过 cancelAll() 来清除状态栏所有的通知。

四、Notification 设置(振动、铃声等) 1. 基本设置: [java] view plaincopy 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. //新建状态栏通知 baseNF = new Notification();

//设置通知在状态栏显示的图标 baseNF.icon = R.drawable.icon;

//通知时在状态栏显示的内容 baseNF.tickerText = "You clicked BaseNF!";

//通知的默认参数 DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS. //如果要全部采用默认值, 用 DEFAULT_ALL. //此处采用默认声音 baseNF.defaults = Notification.DEFAULT_SOUND;

//第二个参数 :下拉状态栏时显示的消息标题 expanded message title //第三个参数:下拉状态栏时显示的消息内容 expanded message text //第四个参数:点击该通知时执行页面跳转 baseNF.setLatestEventInfo(Lesson_10.this, "Title01", "Content01", pd);

//发出状态栏通知 //The first parameter is the unique ID for the Notification // and the second is the Notification object. nm.notify(Notification_ID_BASE, baseNF); 配一张图作说明: 2. 添加声音 如果要采用默认声音,只要使用 default 就可以了。 [java] view plaincopy 1. baseNF.defaults = Notification.DEFAULT_SOUND; 如果要使用自定义声音,那么就要用到 sound 了。如下: [java] view plaincopy 1. notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); 上面这种方法,使用的是自己的铃声,如果想用系统自带的铃声,可以这样: [java] view plaincopy 1. "6"); notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, 需要注意一点,如果 default、sound 同时出现,那么 sound 无效,会使用默认铃声。

默认情况下,通知的声音播放一遍就会结束。

如果你想让声音循环播放,需要为 flags 参 数加上 FLAG_INSISTENT。

这样声音会到用户响应才结束,比如下拉状态栏。 [java] view plaincopy 1. notification.flags |= notification.FLAG_INSISTENT; 3. 添加振动 如果是使用默认的振动方式,那么同样也是使用 default。 [java] view plaincopy 1. notification.defaults |= Notification.DEFAULT_VIBRATE; 当然也可以自己定义振动形式,这边需要用到 Long 型数组。 [java] view plaincopy 1. 2. long[] vibrate = {0,100,200,300};

notification.vibrate = vibrate; 这边的 Long 型数组中,第一个参数是开始振动前等待的时间,第二个参数是第一次振动的 时间,第三个参数是第二次振动的时间,以此类推,随便定义多长的数组。但是采用这种方 法,没有办法做到重复振动。

同样,如果 default、vibrate 同时出现时,会采用默认形式。

另外还需要注意一点:使用振动器时需要权限,如下: [xhtml] view plaincopy 1. > <uses-permission android:name="android.permission.VIBRATE"></uses-permission 4. 闪光 使用默认的灯光,如下: [java] view plaincopy 1. notification.defaults |= Notification.DEFAULT_LIGHTS; 自定义: [java] view plaincopy 1. 2. 3. 4. notification.ledARGB = 0xff00ff00;

notification.ledOnMS = 300;

notification.ledOffMS = 1000;

notification.flags |= Notification.FLAG_SHOW_LIGHTS; 其中 ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间。

注意:这边的颜色跟设备有关,不是所有的颜色都可以,要看具体设备。

5. 其他有用的设置:

flags:

Notification.FLAG_INSISTENT;

//让声音、振动无限循环,直到用户响应 Notification.FLAG_AUTO_CANCEL;

//通知被点击后,自动消失 Notification.FLAG_NO_CLEAR;

//点击'Clear'时,不清楚该通知(QQ 的通知无法清除,就 是用的这个) 下面附上我做的例子,供大家参考。

里面包括创建通知、更新通知、清除通知、设置自定 义铃声、自定义振动、自定义通知视图等。 附上代码:

主类: [java] view plaincopy 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. package com.yfz;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.provider.MediaStore.Audio;

import android.util.Log;

import android.view.View; 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RemoteViews;

import android.widget.SeekBar;

import android.widget.TextView;

/** * Notification * @author Administrator * */ public class Lesson_10 extends Activity { //BaseNotification private Button bt01;

//UpdateBaseNotification private Button bt02;

//ClearBaseNotification private Button bt03;

//MediaNotification private Button bt04;

//ClearMediaNotification private Button bt05;

//ClearALL private Button bt06;

//CustomNotification private Button bt07;

//通知管理器 private NotificationManager nm;

//通知显示内容 private PendingIntent pd;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

/*加载页面*/ setContentView(R.layout.lesson10); 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. init();

} private void init() { bt01 = (Button)findViewById(R.id.le10bt01);

bt02 = (Button)findViewById(R.id.le10bt02);

bt03 = (Button)findViewById(R.id.le10bt03);

bt04 = (Button)findViewById(R.id.le10bt04);

bt05 = (Button)findViewById(R.id.le10bt05);

bt06 = (Button)findViewById(R.id.le10bt06);

bt07 = (Button)findViewById(R.id.le10bt07);

bt01.setOnClickListener(onclick);

bt02.setOnClickListener(onclick);

bt03.setOnClickListener(onclick);

bt04.setOnClickListener(onclick);

bt05.setOnClickListener(onclick);

bt06.setOnClickListener(onclick);

bt07.setOnClickListener(onclick);

nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Intent intent = new Intent(this,Lesson_10.class);

pd = PendingIntent.getActivity(Lesson_10.this, 0, intent, 0);

} OnClickListener onclick = new OnClickListener() { //BASE Notification ID private int Notification_ID_BASE = 110;

private Notification baseNF;

//Notification ID private int Notification_ID_MEDIA = 119;

private Notification mediaNF;

@Override public void onClick(View v) { switch(v.getId()) { case R.id.le10bt01: 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 个) //新建状态栏通知 baseNF = new Notification();

//设置通知在状态栏显示的图标 baseNF.icon = R.drawable.icon;

//通知时在状态栏显示的内容 baseNF.tickerText = "You clicked BaseNF!";

//通知的默认参 //如果要全部采用默认值, 用 DEFAULT_ALL. //此处采用默认声音 baseNF.defaults |= Notification.DEFAULT_SOUND;

baseNF.defaults |= Notification.DEFAULT_VIBRATE;

baseNF.defaults |= Notification.DEFAULT_LIGHTS;

//让声音、振动无限循环,直到用户响应 baseNF.flags |= Notification.FLAG_INSISTENT;

//通知被点击后,自动消失 baseNF.flags |= Notification.FLAG_AUTO_CANCEL;

//点击'Clear'时,不清楚该通知(QQ 的通知无法清除,就是用的这 baseNF.flags |= Notification.FLAG_NO_CLEAR; 数 DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS. 123. 124. 125. 126. 题 expanded message title //第二个参数 :下拉状态栏时显示的消息标 //第三个参数:下拉状态栏时显示的消息内 //第四个参数:点击该通知时执行页面跳转 baseNF.setLatestEventInfo(Lesson_10.this, "Title01", "Co 127. 容 expanded message text 128. 129. ntent01", pd); 130. 131. 132. ion //发出状态栏通知 //The first parameter is the unique ID for the Notificat // and the second is the Notification object. nm.notify(Notification_ID_BASE, baseNF);

break; 133. 134. 135. 136. 137. 138. 139. 140. 提示。 case R.id.le10bt02: //更新通知 //比如状态栏提示有一条新短信,还没来得及查看,又来一条新短信的 //此时采用更新原来通知的方式比较。

//(再重新发一个通知也可以,但是这样会造成通知的混乱,而且显示 baseNF.setLatestEventInfo(Lesson_10.this, "Title02", "Co nm.notify(Notification_ID_BASE, baseNF);

break;

case R.id.le10bt03: //清除 baseNF nm.cancel(Notification_ID_BASE);

break;

case R.id.le10bt04: mediaNF = new Notification();

mediaNF.icon = R.drawable.icon;

mediaNF.tickerText = "You clicked MediaNF!";

//自定义声音 mediaNF.sound = Uri.withAppendedPath(Audio.Media.INTERNA 141. 142. 143. ntent02", pd); 多个通知给用户,对用户也不友好) 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. L_CONTENT_URI, "6"); 160. 161. 162. 163. 164. 165. 166. 167. ontent03", pd); //通知时发出的振动 //第一个参数: 振动前等待的时间 //第二个参数:

第一次振动的时长、以此类推 long[] vir = {0,100,200,300};

mediaNF.vibrate = vir;

mediaNF.setLatestEventInfo(Lesson_10.this, "Title03", "C 168. 169. 170. 171. 172. 173. 174. 175. 176. nm.notify(Notification_ID_MEDIA, mediaNF);

break;

case R.id.le10bt05: //清除 mediaNF nm.cancel(Notification_ID_MEDIA);

break; 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. (), R.layout.custom); case R.id.le10bt06: nm.cancelAll();

break;

case R.id.le10bt07: //自定义下拉视图,比如下载软件时,显示的进度条。

Notification notification = new Notification();

notification.icon = R.drawable.icon;

notification.tickerText = "Custom!";

RemoteViews contentView = new RemoteViews(getPackageName contentView.setImageViewResource(R.id.image, R.drawable. contentView.setTextViewText(R.id.text, "Hello, this mess notification.contentView = contentView;

//使用自定义下拉视图时,不需要再调用 setLatestEventInfo()方 //但是必须定义 contentIntent notification.contentIntent = pd;

nm.notify(3, notification);

break;

} } }; 189. icon); 190. 191. 192. 193. 法 age is in a custom expanded view"); 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. } 主页面: [xhtml] view plaincopy 1. 2. 3. 4. 5. 6. 7. 8. <?xml version="1.0"

encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

<Button android:id="@+id/le10bt01" 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="BaseNotification"

/>

<Button android:id="@+id/le10bt02"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="UpdateBaseNotification"

/>

<Button android:id="@+id/le10bt03"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="ClearBaseNotification"

/>

<Button android:id="@+id/le10bt04"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="MediaNotification"

/>

<Button android:id="@+id/le10bt05"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="ClearMediaNotification"

/>

<Button android:id="@+id/le10bt06"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="ClearALL"

/>

<Button android:id="@+id/le10bt07"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="CustomNotification"

/>

</LinearLayout> 自定义视图页面: [c-sharp] view plaincopy 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. <?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"

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>

Android通知管理 文档贡献者 暂无相关推荐文档 如要投诉违规内容,请到 百度文库投诉中心 ;如要提出功能问题或意见建议,请 点击此处 进行反馈. 暂无评价 | 0人阅读 | 0次下载 ...

统一管理的. 一般来说, 一个Notification应该传送的消息包括: 1 、 一个状态条图标 2... 移除标记为id的通知 (只是针对当前Context下的所有Notification) public void notify(...

在android开发中经常要使用到通知,比如:收到短息,来电等等,通知是应用程序提醒用户的一种方式,他不需要使用Activity。通知向用户传递信息有多种方式:(1)状态栏图标...

 
  • 泥巴往事网(www.nbwtv.com) © 2014 版权所有 All Rights Reserved.