Android Notification(通知)

来源:互联网 发布:绿盟笔试题python 编辑:程序博客网 时间:2024/05/21 14:09

通知基本用法

通知的必要属性

一个通知必须包含以下三项属性:

  • 小图标,对应 setSmallIcon()
  • 通知标题,对应 setContentTitle()
  • 详细信息,对应 setContentText()

其他属性均为可选项,更多属性方法请参考NotificationCompat.Builder。

尽管其他都是可选的,但一般都会为通知添加至少一个动作(Action),这个动作可以是跳转到Activity、启动一个Service或发送一个Broadcas等。 通过以下方式为通知添加动作:

  • 使用PendingIntent
  • 通过大视图通知的 Action Button //仅支持Android 4.1 (API level 16)及更高版本,稍后会介绍

创建通知

1、实例化一个NotificationCompat.Builder对象

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    NotificationCompat.Builder mBuilder = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> NotificationCompat.Builder(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>)                .setSmallIcon(R.drawable.notification_icon)                .setContentTitle(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"My notification"</span>)                .setContentText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Hello World!"</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

NotificationCompat.Builder自动设置的默认值:

  • priority: PRIORITY_DEFAULT
  • when: System.currentTimeMillis()
  • audio stream: STREAM_DEFAULT

2、定义并设置一个通知动作(Action)

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    Intent resultIntent = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Intent(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, ResultActivity.class);    PendingIntent resultPendingIntent = PendingIntent.getActivity(                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);    mBuilder.setContentIntent(resultPendingIntent);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

3、生成Notification对象

Notificatioin notification = mBuilder.build();

4、使用NotificationManager发送通知

// Sets an ID for the notificationint mNotificationId = 001;// Gets an instance of the NotificationManager serviceNotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// Builds the notification and issues it.mNotifyMgr.notify(mNotificationId, notification);

更新通知

更新通知很简单,只需再次发送相同ID的通知即可,如果之前的通知依然存在则会更新通知属性,如果之前通知不存在则重新创建。 
示例代码:

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Sets an ID for the notification, so it can be updated</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> notifyID = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;    NotificationCompat.Builder mNotifyBuilder = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> NotificationCompat.Builder(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>)            .setContentTitle(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"New Message"</span>)            .setContentText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"You've received new messages."</span>)            .setSmallIcon(R.drawable.ic_notify_status);    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> numMessages = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Start of a loop that processes data and then notifies the user</span>    ...        mNotifyBuilder.setContentText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"new content text"</span>)                .setNumber(++numMessages);        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Because the ID remains unchanged, the existing notification is updated.</span>        mNotifyMgr.notify(notifyID, mNotifyBuilder.build());    ...</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>

取消通知

取消通知有如下4种方式:

  • 点击通知栏的清除按钮,会清除所有可清除的通知
  • 设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL的通知,点击该通知时会清除它
  • 通过 NotificationManager 调用 cancel() 方法清除指定ID的通知
  • 通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知

通知类型

大视图通知

通知有两种视图:普通视图和大视图。 
普通视图: 
 
大视图: 

默认情况下为普通视图,可通过NotificationCompat.Builder.setStyle()设置大视图。

注: 大视图(Big Views)由Android 4.1(API level 16)开始引入,且仅支持Android 4.1及更高版本。

构建大视图通知

以上图为例: 
1、构建Action Button的PendingIntent

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    Intent dismissIntent = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Intent(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, PingService.class);    dismissIntent.setAction(CommonConstants.ACTION_DISMISS);    PendingIntent piDismiss = PendingIntent.getService(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, dismissIntent, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);    Intent snoozeIntent = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Intent(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, PingService.class);    snoozeIntent.setAction(CommonConstants.ACTION_SNOOZE);    PendingIntent piSnooze = PendingIntent.getService(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, snoozeIntent, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

2、构建NotificatonCompat.Builder对象

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    NotificationCompat.Builder builder = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> NotificationCompat.Builder(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>)            .setSmallIcon(R.drawable.ic_stat_notification)            .setContentTitle(getString(R.string.notification))            .setContentText(getString(R.string.ping))            .setDefaults(Notification.DEFAULT_ALL) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// requires VIBRATE permission</span>            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 该方法在Android 4.1之前会被忽略</span>            .setStyle(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> NotificationCompat.BigTextStyle()                    .bigText(msg))            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//添加Action Button</span>            .addAction (R.drawable.ic_stat_dismiss,                    getString(R.string.dismiss), piDismiss)            .addAction (R.drawable.ic_stat_snooze,                    getString(R.string.snooze), piSnooze);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

3、其他步骤与普通视图相同

进度条通知

  • 明确进度的进度条 
    使用setProgress(max, progress, false)来更新进度。 
    max: 最大进度值 
    progress: 当前进度 
    false: 是否是不明确的进度条

    模拟下载过程,示例如下:

    <code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> id = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;    ...    mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);    mBuilder = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> NotificationCompat.Builder(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>);    mBuilder.setContentTitle(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Picture Download"</span>)        .setContentText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Download in progress"</span>)        .setSmallIcon(R.drawable.ic_notification);    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Start a lengthy operation in a background thread</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Thread(        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Runnable() {            <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">run</span>() {                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> incr;                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (incr = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; incr <= <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>; incr+=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>) {                    mBuilder.setProgress(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, incr, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>);                    mNotifyManager.notify(id, mBuilder.build());                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> {                        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Sleep for 5 seconds</span>                        Thread.sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>);                    } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (InterruptedException e) {                        Log.d(TAG, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sleep failure"</span>);                    }                }                mBuilder.setContentText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Download complete"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//下载完成           </span>                        .setProgress(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>);    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//移除进度条</span>                mNotifyManager.notify(id, mBuilder.build());            }        }    ).start();</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li></ul>

     
    上图,分别为下载过程中进度条通知 和 下载完成移除进度条后的通知。

  • 不确定进度的进度条 
    使用setProgress(0, 0, true)来表示进度不明确的进度条

    mBuilder.setProgress(0, 0, true); mNotifyManager.notify(id, mBuilder.build()); 

浮动通知(Heads-up Notifications)

Android 5.0(API level 21)开始,当屏幕未上锁且亮屏时,通知可以以小窗口形式显示。用户可以在不离开当前应用前提下操作该通知。 
如图: 

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this)        .setContentTitle("New Message")        .setContentText("You've received new messages.")        .setSmallIcon(R.drawable.ic_notify_status)        .setFullScreenIntent(pendingIntent, false);

以下两种情况会显示浮动通知:

  • setFullScreenIntent(),如上述示例。
  • 通知拥有高优先级且使用了铃声和振动

锁屏通知

Android 5.0(API level 21)开始,通知可以显示在锁屏上。用户可以通过设置选择是否允许敏感的通知内容显示在安全的锁屏上。 
你的应用可以通过setVisibility()控制通知的显示等级:

  • VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容
  • VISIBILITY_PUBLIC : 显示通知的全部内容
  • VISIBILITY_SECRET : 不显示任何内容,包括图标

自定义通知

Android系统允许使用RemoteViews来自定义通知。 
自定义普通视图通知高度限制为64dp,大视图通知高度限制为256dp。同时,建议自定义通知尽量简单,以提高兼容性。

自定义通知需要做如下操作: 
1、创建自定义通知布局 
2、使用RemoteViews定义通知组件,如图标、文字等 
3、调用setContent()将RemoteViews对象绑定到NotificationCompat.Builder 
4、同正常发送通知流程

注意: 避免为通知设置背景,因为兼容性原因,有些文字可能看不清。

定义通知文本样式

通知的背景颜色在不同的设备和版本中有所不同,Android2.3开始,系统定义了一套标准通知文本样式,建议自定义通知使用标准样式,这样有助于通知文本可见。 
通知文本样式:

Android 5.0之前可用:android:style/TextAppearance.StatusBar.EventContent.Title    // 通知标题样式  android:style/TextAppearance.StatusBar.EventContent             // 通知内容样式  Android 5.0及更高版本:  android:style/TextAppearance.Material.Notification.Title         // 通知标题样式  android:style/TextAppearance.Material.Notification                  // 通知内容样式  

更多通知的标准样式和布局,可参考源码frameworks/base/core/res/res/layout路径下的通知模版如:

Android 5.0之前:  notification_template_base.xml  notification_template_big_base.xml  notification_template_big_picture.xml  notification_template_big_text.xml  Android 5.0 及更高版本:  notification_template_material_base.xml  notification_template_material_big_base.xml  notification_template_material_big_picture.xml  notification_template_part_chronometer.xml  notification_template_progressbar.xml  等等。

保留Activity返回栈

常规Activity

默认情况下,从通知启动一个Activity,按返回键会回到主屏幕。但某些时候有按返回键仍然留在当前应用的需求,这就要用到TaskStackBuilder了。

1、在manifest中定义Activity的关系

Android 4.0.3 及更早版本<activity    android:name=".ResultActivity">    <meta-data        android:name="android.support.PARENT_ACTIVITY"        android:value=".MainActivity"/></activity>Android 4.1 及更高版本<activity    android:name=".ResultActivity"    android:parentActivityName=".MainActivity"></activity>

2、创建返回栈PendingIntent

Intent resultIntent = new Intent(this, ResultActivity.class);TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);// 添加返回栈stackBuilder.addParentStack(ResultActivity.class);// 添加Intent到栈顶stackBuilder.addNextIntent(resultIntent);// 创建包含返回栈的pendingIntentPendingIntent resultPendingIntent =        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);NotificationCompat.Builder builder = new NotificationCompat.Builder(this);builder.setContentIntent(resultPendingIntent);NotificationManager mNotificationManager =    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);mNotificationManager.notify(id, builder.build());

上述操作后,从通知启动ResultActivity,按返回键会回到MainActivity,而不是主屏幕。

特殊Activity

默认情况下,从通知启动的Activity会在近期任务列表里出现。如果不需要在近期任务里显示,则需要做以下操作:

1、在manifest中定义Activity

<activity    android:name=".ResultActivity"    android:launchMode="singleTask"    android:taskAffinity=""    android:excludeFromRecents="true"></activity>

2、构建PendingIntent

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);Intent notifyIntent = new Intent(this, ResultActivity.class);// Sets the Activity to start in a new, empty tasknotifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);PendingIntent notifyPendingIntent =        PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(notifyPendingIntent);NotificationManager mNotificationManager =    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);mNotificationManager.notify(id, builder.build());

上述操作后,从通知启动ResultActivity,此Activity不会出现在近期任务列表中。

通知常见属性和常量

通知的提醒方式

1、声音提醒

  • 默认声音 
    notification.defaults |= Notification.DEFAULT_SOUND;

  • 自定义声音 
    notification.sound = Uri.parse(“file:///sdcard0/notification.ogg”);

2、震动提醒

  • 默认振动 
    notification.defaults |= Notification.DEFAULT_VIBRATE;

  • 自定义振动 
    long[] vibrate = {100, 200, 300, 400}; //震动效果 
    // 表示在100、200、300、400这些时间点交替启动和关闭震动 notification.vibrate = vibrate;

3、闪烁提醒

  • 默认闪烁 
    notification.defaults |= Notification.DEFAULT_LIGHTS;

  • 自定义闪烁 
    notification.ledARGB = 0xff00ff00; // LED灯的颜色,绿灯 
    notification.ledOnMS = 300; // LED灯显示的毫秒数,300毫秒 
    notification.ledOffMS = 1000; // LED灯关闭的毫秒数,1000毫秒 
    notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必须加上这个标志

常见的Flags

  • FLAG_AUTO_CANCEL 
    当通知被用户点击之后会自动被清除(cancel)

  • FLAG_INSISTENT 
    在用户响应之前会一直重复提醒音

  • FLAG_ONGOING_EVENT 
    表示正在运行的事件

  • FLAG_NO_CLEAR 
    通知栏点击“清除”按钮时,该通知将不会被清除

  • FLAG_FOREGROUND_SERVICE 
    表示当前服务是前台服务

0 0
原创粉丝点击