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实现例子的更多相关文章

  1. Android自定义Notification并没有那么简单

    背景 最近需要实现一个自定义Notification的功能.网上找了找代码,解决方案就是通过RemoteViews来实现.但是在实现过程中遇到不少问题,网上也没有很好的文章描述这些问题,所以在这里做个 ...

  2. Android高德地图自定义Markers的例子

    下文为各位重点介绍关于Android高德地图自定义Markers的例子,希望这篇文章能够让各位理解到Android高德地图自定义Markers的方法. 之前的博客里说了地图的嵌入和定位,今天就说说在地 ...

  3. Android -- 系统和自定义Notification

    Notification是一种让你的应用程序在不使用Activity的情况下警示用户,Notification是看不见的程序组件警示用户有需要注意的事件发生的最好途径. 作为UI部分,Notifica ...

  4. oracle的order by decode根据文字自定义排序的例子

    oracle的order by decode根据文字自定义排序的例子: order by decode(t.title, '当前生效预警', 1, '今日即将生效', 2, '明日预计生效', 3, ...

  5. android:使用RemoteView自定义Notification

    //网上相关内容较少,遂记录下来,备忘. //依然以音乐播放器demo为例. 效果截图 //锤子手机上的效果 step1 准备自定义layout 常规的实现方式,并不会因为是用于notificatio ...

  6. android自定义Notification通知栏实例

    项目有个需求,需要在发送Notification的时候动态给定url的图片.大概思路如下:自己定义一个Notification的布局文件,这样能够很方便设置View的属性. 首先加载网络图片,使用Bi ...

  7. android显示通知栏Notification以及自定义Notification的View

    遇到的最大的问题是监听不到用户清除通知栏的广播.所以是不能监听到的. 自定义通知栏的View,然后service运行时更改notification的信息. /** * Show a notificat ...

  8. 自定义Notification

    private static void updateProgressNotification(Context cxt, int appsCount, int percent, String appNa ...

  9. 一个基于MBProgressHUD的自定义视图hud例子

    项目中用到的一个hud,基于MBProgressHUD,使用自定义视图实现的,动画效果是从网上参考的,并不是很理想.有需要的可以看看,这里是源码(源码用了cocoapods,运行前需要pod inst ...

随机推荐

  1. tomcat无法正确解析请求参数

    24-Mar-2018 14:11:20.564 INFO [http-nio-8080-exec-3] org.apache.coyote.http11.Http11Processor.servic ...

  2. mysql explain的使用

    一.explain返回各列的含义: 1.table:显示这一行的数据是关于那张表的 2.type:重要的列,显示连接使用了何种类型,从最好到最差的连接类型为const.eq_reg.ref.range ...

  3. Nginx基础篇(2)- Nginx基本配置文件和变量详解

    Nginx基本配置文件和变量详解 1. 基本配置文件 /etc/nginx/nginx.conf # nginx运行的用户 user nginx; # nginx进程数,建议设置为等于CPU总核心数. ...

  4. Bootstrap-Table 总结

    Bootstrap-Table 总结 jQuery Java Bootstrap-Table JS文件 传参:直接将需要的参数置于 queryParams 方法中,例如 line:formData注意 ...

  5. winform ComboBox/TextBox自动提示

    ComboBox和TextBox控件都带有自动前缀匹配,只要设置其中的AutoCompleteMode,AutoCompleteSource,AutoCompleteCustomSource三个属性的 ...

  6. 正则表达式 整理(\w \s \d 点 贪婪匹配 非贪婪匹配 * + ? {} | [] ^ $ \b 单词边界 分组、re.findall()、re.split()、re.search()、re.match()、re.compile()、re.sub())

    re.findall  匹配到正则表达式的字符,匹配到的每个字符存入一个列表,返回一个匹配到的所有字符列表 一. 匹配单个字符 import re # \w 匹配所有字母.数字.下划线 re.find ...

  7. 91-Williams' Percent Range 威廉指标.(2015.7.4)

    Williams' Percent Range 威廉指标 ~计算: %R = (HIGH(i-n)-CLOSE)/(HIGH(i-n)-LOW(i-n))×100 注解:CLOSE: 当前时段的收盘价 ...

  8. STM32——通用定时器基本定时功能

    STM32——————通用定时器基本定时功能                                                                           1.  ...

  9. UVA 221 城市化地图(离散化思想)

    题意: 给出若干个栋楼俯视图的坐标和面积,求从俯视图的南面(可以视为正视图)看过去到底能看到多少栋楼. 输入第一个n说明有n栋楼,然后输入5个实数(注意是实数),分别是楼的左下角坐标(x,y), 然后 ...

  10. HDU 2147 找规律博弈

    题目大意: 从右上角出发一直到左下角,每次左移,下移或者左下移,到达左下角的人获胜 到达左下角为必胜态,那么到达它的所有点都为必败态,每个点的局势都跟左,下,左下三个点有关 开始写了一个把所有情况都计 ...