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. mysql崩溃恢复

    mysql进程崩溃. 杀掉所有mysql进程,在my.cnf文件中写入innodb_recover_force=1,强制并忽略任何错误启动数据库. 用mysqldump导出所有数据,在新机器上部署好m ...

  2. ResNet实战

    目录 Res Block ResNet18 Out of memory # Resnet.py #!/usr/bin/env python # -*- coding:utf-8 -*- import ...

  3. UVA 213 信息解码(二进制&位运算)

    题意: 出自刘汝佳算法竞赛入门经典第四章. 考虑下面的01串序列: 0, 00, 01, 10, 000, 001, 010, 011, 100, 101, 110, 0000, 0001, …, 1 ...

  4. Rim 边缘光

    边缘光:计算眼睛和模型顶点法线的点积,结果作为强度,和材质输出:顶点和法线平行时,强度最大,垂直时,强度最小.因此将他取反,即同一方向时,强度最小,垂直时,强度最大. -dot(normalize(v ...

  5. CodeForcesGym 100517B Bubble Sort

    Bubble Sort Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...

  6. Android BottomSheet:List列表或Grid网格展示(3)

     Android BottomSheet:List列表或Grid网格展示(3) BottomSheet可以显示多种样式的底部弹出面板风格,比如常见的List列表样式或者Grid网格样式,以一个例子 ...

  7. codeforces 362B

    #include<stdio.h> #include<stdlib.h> int cmp(const void *a,const void *b) { return *(int ...

  8. poj3440--Coin Toss(几何上的概率)

    Coin Toss Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3946   Accepted: 1076 Descrip ...

  9. 【NOIP2016】蚯蚓(单调队列)

    题意: 思路: 我们发现,对于任意两次切割i和j,i<j,在进行完第j次切割后,第i次切割的u/v部分一定大于等于第j次切割的u/v部分,第i次的1-u/v部分也一定大于等于第j次的1-u/v部 ...

  10. 【HDOJ5714】拍照(线性扫描)

    题意:小明在旅游的路上看到了一条美丽的河,河上有许多船只,有的船只向左航行,有的船只向右航行.小明希望拍下这一美丽的风景,并且把尽可能多的船只都完整地拍到一张照片中. 小明位于河的边上,并且可以在河边 ...