自定义Notification实现例子
1.自定义view:
<?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:background="@null"
android:gravity="center_vertical"
android:orientation="horizontal" > <ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginRight="10dp"
android:contentDescription="@null" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@null"
android:gravity="center_vertical"
android:orientation="vertical" > <TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp" /> <TextView
android:id="@+id/text02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#999999"
android:textSize="15sp" />
</LinearLayout> </LinearLayout>
2.实现代码:
public static void plx(Context context, Ad ad) {
try {
//1.创建一个NotificationManager的引用
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//定义Notification的各种属性
int statusBarIcon = android.R.drawable.sym_action_email;//设置在状态栏中的图片id
// int statusBarIcon = android.R.drawable.sym_action_chat;//设置在状态栏中的图片id
// int statusBarIcon = android.R.drawable.sym_def_app_icon;//设置在状态栏中的图片id
String statusBarText = ad.getName();//在状态栏上展示的滚动信息
long statusBarTime = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
//用上面的属性初始化Nofification
Notification notification = new Notification(statusBarIcon, statusBarText, statusBarTime);
//2.设置flags属性
notification.flags = Notification.FLAG_AUTO_CANCEL;//该标志表示当用户点击 Clear 之后,能够清除该通知
// notification.flags = Notification.FLAG_NO_CLEAR;
// notification.flags = Notification.FLAG_ONGOING_EVENT;//出现在 “正在运行的”栏目下面
//3、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.lx_view);
contentView.setImageViewResource(R.id.image,android.R.drawable.sym_action_email);
contentView.setTextViewText(R.id.text01,ad.getName());
contentView.setTextViewText(R.id.text02,ad.getAdwords());
notification.contentView = contentView;
//4.为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
Intent intent = new Intent(context, DdService.class);
intent.setAction(DdService.class.getName() + ad.getId());
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Keys.id, ad.getId());
intent.putExtra(Keys.downloadUrl, ad.getDownloadUrl());
intent.putExtra(Keys.name, ad.getName());
intent.putExtra(Keys.adwords, ad.getAdwords());
intent.putExtra(Keys.version, ad.getVersion());
LogUtil.i(tag, "id=" + ad.getId());
LogUtil.i(tag, "downloadUrl=" + ad.getDownloadUrl());
LogUtil.i(tag, "name=" + ad.getName());
LogUtil.i(tag, "adwords=" + ad.getAdwords());
LogUtil.i(tag, "version=" + ad.getVersion());
PendingIntent pendingIntent = PendingIntent.getService(
context,
0, //发送者的请求码(可以填0)
intent, //用于系统发送的Intent
PendingIntent.FLAG_UPDATE_CURRENT);//标志位, 其中 PendingIntent.FLAG_UPDATE_CURRENT 表示如果该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据
notification.contentIntent = pendingIntent;
//5.把Notification传递给NotificationManager
int notificationId = Integer.valueOf(ad.getId());//用来区分同一程序中的不同Notifycation
LogUtil.i(tag, "notificationId=" + notificationId);
notificationManager.notify(notificationId, notification);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void download(Context context, String progress, String title, String fileName, int notificationId) {
try {
//1.创建一个NotificationManager的引用
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//定义Notification的各种属性
int statusBarIcon = android.R.drawable.stat_sys_download;//现在在状态栏中的图片id
String statusBarText = "正在下载...";//在状态栏上展示的滚动信息
long statusBarTime = System.currentTimeMillis();//时间
Notification notification = new Notification(statusBarIcon, statusBarText, statusBarTime);
//2.设置flags属性
notification.flags = Notification.FLAG_AUTO_CANCEL;//该标志表示当用户点击 Clear 之后,能够清除该通知
// notification.flags = Notification.FLAG_NO_CLEAR;
// notification.flags = Notification.FLAG_ONGOING_EVENT;//出现在 “正在运行的”栏目下面
//3、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
//设置显示在通知下拉框中的信息,参数依次为:Context,标题,内容,PendingIntent
String content = "正在下载... " + progress;
if(progress.contains("100%")) {
LogUtil.i(tag, "progress2=" + progress);
content = "下载完成,点击安装!";
}
RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.lx_view);
contentView.setImageViewResource(R.id.image,android.R.drawable.stat_sys_download);
contentView.setTextViewText(R.id.text01, title);
contentView.setTextViewText(R.id.text02, content);
notification.contentView = contentView;
//4.为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
Intent intent = new Intent();
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
if(progress.contains("100%")) {
LogUtil.i(tag, "progress=" + progress);
intent.setClass(context, InstallApkService.class);
intent.putExtra(Keys.id, notificationId + "");
intent.putExtra(Keys.title, title);
intent.putExtra(Keys.fileName, fileName);
intent.setAction(InstallApkService.class.getName() + notificationId);
}
PendingIntent pendingIntent = PendingIntent.getService(
context,
0, //发送者的请求码(可以填0)
intent, //用于系统发送的Intent
PendingIntent.FLAG_UPDATE_CURRENT);//标志位, 其中 PendingIntent.FLAG_UPDATE_CURRENT 表示如果该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据
notification.contentIntent = pendingIntent;
//5.把Notification传递给NotificationManager
notificationManager.notify(notificationId, notification);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void cancel(Context context, int notificationId) {
try {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);
} catch (Exception e) {
e.printStackTrace();
}
}
自定义Notification实现例子的更多相关文章
- Android自定义Notification并没有那么简单
背景 最近需要实现一个自定义Notification的功能.网上找了找代码,解决方案就是通过RemoteViews来实现.但是在实现过程中遇到不少问题,网上也没有很好的文章描述这些问题,所以在这里做个 ...
- Android高德地图自定义Markers的例子
下文为各位重点介绍关于Android高德地图自定义Markers的例子,希望这篇文章能够让各位理解到Android高德地图自定义Markers的方法. 之前的博客里说了地图的嵌入和定位,今天就说说在地 ...
- Android -- 系统和自定义Notification
Notification是一种让你的应用程序在不使用Activity的情况下警示用户,Notification是看不见的程序组件警示用户有需要注意的事件发生的最好途径. 作为UI部分,Notifica ...
- oracle的order by decode根据文字自定义排序的例子
oracle的order by decode根据文字自定义排序的例子: order by decode(t.title, '当前生效预警', 1, '今日即将生效', 2, '明日预计生效', 3, ...
- android:使用RemoteView自定义Notification
//网上相关内容较少,遂记录下来,备忘. //依然以音乐播放器demo为例. 效果截图 //锤子手机上的效果 step1 准备自定义layout 常规的实现方式,并不会因为是用于notificatio ...
- android自定义Notification通知栏实例
项目有个需求,需要在发送Notification的时候动态给定url的图片.大概思路如下:自己定义一个Notification的布局文件,这样能够很方便设置View的属性. 首先加载网络图片,使用Bi ...
- android显示通知栏Notification以及自定义Notification的View
遇到的最大的问题是监听不到用户清除通知栏的广播.所以是不能监听到的. 自定义通知栏的View,然后service运行时更改notification的信息. /** * Show a notificat ...
- 自定义Notification
private static void updateProgressNotification(Context cxt, int appsCount, int percent, String appNa ...
- 一个基于MBProgressHUD的自定义视图hud例子
项目中用到的一个hud,基于MBProgressHUD,使用自定义视图实现的,动画效果是从网上参考的,并不是很理想.有需要的可以看看,这里是源码(源码用了cocoapods,运行前需要pod inst ...
随机推荐
- 17Spring前置通知
1).加入jar包:下载地址 spring-beans-4.1.6.RELEASE.jar commons-logging-1.1.3.jar spring-context-4.1.6.RELEASE ...
- CSS Specificity(特殊性)
CSS的特殊性是非常重要却又经常被忽视的属性,特别是在团队合作下的产品迭代开发中,因为不注重CSS的特殊性最后导致某些代码混乱不堪,这里就把自己对CSS特殊性的认识做一些归纳总结. CSS的特殊性(s ...
- Django之初
Django之初 Django的开始: #安装Django: pip3 install django #创建Django项目: django-admin startproject 项目名 #比如: d ...
- LeetCode(64) Minimum Path Sum
题目 Total Accepted: 47928 Total Submissions: 148011 Difficulty: Medium Given a m x n grid filled with ...
- 杭电 2035 (快速幂) 求A^B的最后三位数表示的整数
Description 求A^B的最后三位数表示的整数. 说明:A^B的含义是“A的B次方” Input 输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B&l ...
- 【Codeforces 1031C】Cram Time
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 如果找到最大的n使得1+2+...+n<=a+b 然后第一天输出1,2.3,4....t1 这里1+2+..+t1<=a 这还远远 ...
- POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28838 Accepted: 9843 -& ...
- 【bzoj3747】[POI2015]Kinoman - 线段树(经典)
Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[i]部. 你可以选择l,r(1<=l< ...
- DEA中MAVEN项目有多个子目录,如何加载构建
ddts这个项目有三个子目录,每个子目录下面也都有一个 pom.xml 此时需要 右键子目录的 pom.xml,选择Add as Maven Project,在上图中cli.core两个目 ...
- 关于PHP include文件时的文件查找顺序
常常被include文件的路径搞晕. 看来是要理一理的时候了. PHP官方文档关于include搜索路径的解释是:先查找工作目录下相对于include_path设置所对应的路径,然后再搜索执行文件所在 ...