Notification在android中的使用比较多,比如消息推送,使用系统的下载管理器下载,音乐播放器最小化......
现在总结一下Notificaiton的使用
[java] view plaincopy
public void initNotification() {
Intent intent = new Intent(this, ResultActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
/**
* getActivity(Context context, int requestCode, Intent intent, int flags)
* 这个方法的作用就相当于context.startActivity(intent)
*/
PendingIntent pendingintent = PendingIntent.getActivity(this, 0,
intent, 0);
//第二个参数表示notificaiton刚刚 显示在状态栏时候显示的文字
Notification notification = new Notification(R.drawable.ic_launcher,
"first show", 0);
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.notificationlayout);
notification.contentIntent = pendingintent;
notification.contentView = remoteViews;
// Notification.FLAG_ONGOING_EVENT 设置notification显示在正在运行栏中,默认是显示在通知栏里面
notification.flags = Notification.FLAG_AUTO_CANCEL
| Notification.FLAG_ONGOING_EVENT;
NotificationManager manager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
// 这个方法执行时候 如果该ID对象的notificaiton对象已经显示在状态栏了,那么就用新的notificaiton对象更新之前的那个
manager.notify(ID, notification);
}
这是最常见的使用方法。
android后来又出来一个NotificationCompat.Builder用来构造Notificaiton
官方网址:http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setProgress(int, int, boolean)
这个类提供了很多方法用于构造notificaiton。
下面是例子:
[java] view plaincopy
public void initNotificationNew() {
NotificationManager manager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, ResultActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingintent = PendingIntent.getActivity(this, 0,
intent, 0);
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.notificationlayout);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
//setSmallIcon setContentTitle setContentText是必须的 不定义或少定义都不显示notificaiton
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("NotificationCompat")
.setContentText("NotificationCompat Text")
.setContentIntent(pendingintent).setNumber(10).setOngoing(true)
.setWhen(0).setAutoCancel(true).setContent(remoteViews);
//这个方法是显示进度条,这个比较有用
builder.setProgress(100, progress, false);
manager.notify(ID, builder.build());
}
NotificationCompat.Builder还有一个setStyle方法用于设置一个新的界面显示效果,但是好像只支持4.1+系统,使用不多。就不多说了。
分享到:
相关推荐
iphone notification 使用实例,简要的阐明了notification使用的方法,需要的可以下一下
Android NOtification 使用Android NOtification 使用
Notification 使用详解(很全)Notification 使用详解(很全)
本文实例讲解了通知Notification使用方法,此知识点就是用作通知的显示,包括振动、灯光、声音等效果,分享给大家供大家参考,具体内容如下 效果图: MainActivity: import java.io.File; import android....
使用android做的小例子 可以在通知栏显示消息和小图标 点击进去可以进入一个页面 主要用于notification的学习 基于android2.3开发
通知的使用大全,自定义等多方面的通知样式
在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长时间显示的信息,用Notification就最合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态...
Push Notification,为了是手机应用 keep onlive ,经常会有一个一直运转的后台服务程序,这样的应用使手机特别耗电,现在用了 Push Notification 可以来处理这个问题。
Android Notification使用详解 Notification 核心代码(链式调用):适用于Android 4.0以上(不兼容低版本) Notification noti = new Notification.Builder(this) .setContentTitle(标题名称) .setContentText...
NULL 博文链接:https://zhanhao.iteye.com/blog/1176145
主要介绍了Android Notification 使用方法详解的相关资料,这里提供实例来帮助大家理解掌握这部分内容,需要的朋友可以参考下
讲述关于notification的全面使用指南,通俗易懂的资料,适合新手
主要介绍了Android Notification使用方法总结的相关资料,这里提供了四种使用方法,需要的朋友可以参考下
如果您不会使用,请查看《WordPress Comment Reply Notification 使用教程》。 Plugin Name: Comment Reply Notification Plugin URI: ...
@ dafcoe / vue-notification 使用Vue3构建的易于使用,可自定义的通知/吐司库。 在页面上查看操作。安装使用yarn或npm将软件包安装为项目依赖项: $ yarn add @dafcoe/vue-notification--- or ---$ npm install --...
Notification的使用示例各种效果,包含系统一般notification的示例,以及自定义铃声震动等
rc通知React Notification UI组件安装用法import Notification from 'rc-notification' ;Notification . newInstance ( { } , notification => { notification . notice ( { content : 'content' } ) ;} ) ;兼容性 ...