1 Notification简介

最后消息发送:

NotificationManager notificationManager;
Notification notification;
NotificationComapt.Builder cBuilder;
//Notification.Builder nBuillder;  大文本时用到 此处只列出NotificationComap.Builder//获取服务
notificationManager=(NotificationManager)
context.getSystemService(Activity.NOTIFICATION_SERVICE);
//内容设置
cBuilder = new NotificationComapt.Builder(context);
cBuilder.setContentIntent(pendingIntent); //使用pendingIntent下载apk,消息推送,等等
cBuilder.setSmallIcon(smallIcon); //设置刚收到消息时显示的小图标
cBuilder.setTiker(ticker); //设置刚收到消息时显示的消息提示
cBuilder.setContentTitle(title); //设置在详细通知栏中显示的标题
cBuilder.setContentText(content); //设置在详细通知栏中显示的内容信息
…..
//发送通知
notification = cBuilder.build();
notificationManager.notify(NOTIFICATION_ID, notification);

2 通知栏下载apk功能

该demo下载功能依赖于afinal.jar。

  • MainActivity.java
package com.ycl.service_download;import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;public class MainActivity extends AppCompatActivity {String download_url="http://shouji.360tpcdn.com/160329/a9037075b8d3aa98fbf6115c54a5b895/com.alensw.PicFolder_4722404.apk";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void bt_start_service(View view){Intent intent=new Intent(this,DownLoadService.class);intent.putExtra("download_url",download_url);startService(intent);}
}
  • DownloadService.java
package com.ycl.service_download;import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;import java.io.File;public class DownLoadService extends Service {String download_url;String savePath= Environment.getExternalStorageDirectory()+"/liulan.apk";private int requestCode = (int) SystemClock.uptimeMillis();private NotifyUtil currentNotify;File mFile;@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {mFile=new File(savePath);download_url=intent.getStringExtra("download_url");Log.e("test","执行onStartCommand");//设置想要展示的数据内容Intent intent_noti = new Intent();intent_noti.setAction(Intent.ACTION_VIEW);//文件的类型,从tomcat里面找intent_noti.setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive");PendingIntent rightPendIntent = PendingIntent.getActivity(this,requestCode, intent_noti, PendingIntent.FLAG_UPDATE_CURRENT);int smallIcon = R.drawable.xc_smaillicon;String ticker = "正在更新快图浏览";//实例化工具类,并且调用接口NotifyUtil notify7 = new NotifyUtil(this, 7);notify7.notify_progress(rightPendIntent, smallIcon, ticker, "快图浏览升级程序", "正在下载中",false, false, false, download_url, savePath, new NotifyUtil.DownLoadListener() {@Overridepublic void OnSuccess(File file) {mFile=file;DownLoadService.this.stopSelf();}@Overridepublic void onFailure(Throwable t, int errorNo, String strMsg) {}});currentNotify = notify7;return super.onStartCommand(intent, flags, startId);}
}
  • NotifyUtils
package com.vlike.util;import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.SystemClock;
import android.support.v7.app.NotificationCompat;
import android.widget.RemoteViews;
import android.widget.Toast;import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.HttpHandler;import java.io.File;
import java.util.ArrayList;@SuppressLint("NewApi")
public class NotifyUtils {private static int NOTIFICATION_ID;private NotificationManager notificationManager;private Notification notification;private NotificationCompat.Builder cBuilder;private Notification.Builder nBuilder;private Context mContext;public NotifyUtils(Context mContext, int NOTIFICATION_ID) {this.NOTIFICATION_ID = NOTIFICATION_ID;this.mContext = mContext;notificationManager = (NotificationManager) mContext.getSystemService(Activity.NOTIFICATION_SERVICE);cBuilder = new NotificationCompat.Builder(mContext);}/*** 设置在顶部通知栏中的各种信息** @param pendingIntent* @param smallIcon* @param ticker*/public void setCompatBuilder(PendingIntent pendingIntent, int smallIcon, String ticker,String title, String content, boolean sound, boolean vibrate, boolean lights) {cBuilder.setContentIntent(pendingIntent);cBuilder.setSmallIcon(smallIcon);cBuilder.setTicker(ticker);cBuilder.setContentTitle(title);cBuilder.setContentText(content);cBuilder.setWhen(System.currentTimeMillis());cBuilder.setAutoCancel(true);cBuilder.setPriority(NotificationCompat.PRIORITY_MAX);int defaults = 0;if (sound) {defaults |= Notification.DEFAULT_SOUND;}if (vibrate) {defaults |= Notification.DEFAULT_VIBRATE;}if (lights) {defaults |= Notification.DEFAULT_LIGHTS;}cBuilder.setDefaults(defaults);}/*** 设置builder的信息,在用大文本时会用到这个** @param pendingIntent* @param smallIcon* @param ticker*/private void setBuilder(PendingIntent pendingIntent, int smallIcon, String ticker, boolean sound, boolean vibrate, boolean lights) {nBuilder = new Notification.Builder(mContext);nBuilder.setContentIntent(pendingIntent);nBuilder.setSmallIcon(smallIcon);nBuilder.setTicker(ticker);nBuilder.setWhen(System.currentTimeMillis());nBuilder.setPriority(Notification.PRIORITY_MAX);int defaults = 0;if (sound) {defaults |= Notification.DEFAULT_SOUND;}if (vibrate) {defaults |= Notification.DEFAULT_VIBRATE;}if (lights) {defaults |= Notification.DEFAULT_LIGHTS;}nBuilder.setDefaults(defaults);}/*** 普通的通知* <p/>* 1. 侧滑即消失,下拉通知菜单则在通知菜单显示** @param pendingIntent* @param smallIcon* @param ticker* @param title* @param content*/public void notifyNormalSingleline(PendingIntent pendingIntent, int smallIcon,String ticker, String title, String content, boolean sound, boolean vibrate, boolean lights) {setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);sent();}/*** 进行多项设置的通知(在小米上似乎不能设置大图标,系统默认大图标为应用图标)** @param pendingIntent* @param smallIcon* @param ticker* @param title* @param content*/public void notifyMailbox(PendingIntent pendingIntent, int smallIcon, int largeIcon, ArrayList<String> messageList,String ticker, String title, String content, boolean sound, boolean vibrate, boolean lights) {setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), largeIcon);cBuilder.setLargeIcon(bitmap);cBuilder.setDefaults(Notification.DEFAULT_ALL);cBuilder.setAutoCancel(true);NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();for (String msg : messageList) {inboxStyle.addLine(msg);}inboxStyle.setSummaryText("[" + messageList.size() + "条]" + title);cBuilder.setStyle(inboxStyle);sent();}/*** 自定义视图的通知** @param remoteViews* @param pendingIntent* @param smallIcon* @param ticker*/public void notifyCustomView(RemoteViews remoteViews, PendingIntent pendingIntent,int smallIcon, String ticker, boolean sound, boolean vibrate, boolean lights) {setCompatBuilder(pendingIntent, smallIcon, ticker, null, null, sound, vibrate, lights);notification = cBuilder.build();notification.contentView = remoteViews;notificationManager.notify(NOTIFICATION_ID, notification);}/*** 可以容纳多行提示文本的通知信息 (因为在高版本的系统中才支持,所以要进行判断)** @param pendingIntent* @param smallIcon* @param ticker* @param title* @param content*/public void notifyNormalMoreline(PendingIntent pendingIntent, int smallIcon, String ticker,String title, String content, boolean sound, boolean vibrate, boolean lights) {final int sdk = Build.VERSION.SDK_INT;if (sdk < Build.VERSION_CODES.JELLY_BEAN) {notifyNormalSingleline(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);Toast.makeText(mContext, "您的手机低于Android 4.1.2,不支持多行通知显示!!", Toast.LENGTH_SHORT).show();} else {setBuilder(pendingIntent, smallIcon, ticker, true, true, false);nBuilder.setContentTitle(title);nBuilder.setContentText(content);nBuilder.setPriority(Notification.PRIORITY_HIGH);notification = new Notification.BigTextStyle(nBuilder).bigText(content).build();notificationManager.notify(NOTIFICATION_ID, notification);}}/*** 有进度条的通知,可以设置为模糊进度或者精确进度** @param pendingIntent* @param smallIcon* @param ticker* @param title* @param content*/public void notifyProgress(PendingIntent pendingIntent, int smallIcon,String ticker, String title, String content,boolean sound, boolean vibrate, boolean lights,String download_url, String savePath, final DownLoadListener listener) {setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);FinalHttp fh = new FinalHttp();HttpHandler<File> httpHandler=fh.download(download_url,  savePath, new AjaxCallBack<File>() {@Overridepublic void onLoading(long count, long current) {super.onLoading(count, current);double a=count;double b=current;double currentPro=(double)((b/a)*100);cBuilder.setProgress(100, (int)currentPro, false);SystemClock.sleep(300); //加上延缓,加强体验效果sent();}@Overridepublic void onSuccess(File file) {super.onSuccess(file);cBuilder.setContentText("下载完成").setProgress(0, 0, false);sent();listener.OnSuccess(file);}@Overridepublic void onFailure(Throwable t, int errorNo, String strMsg) {super.onFailure(t, errorNo, strMsg);listener.onFailure(t,errorNo,strMsg);}});}/*** 容纳大图片的通知** @param pendingIntent* @param smallIcon* @param ticker* @param title* @param bigPic*/public void notifyBigPic(PendingIntent pendingIntent, int smallIcon, String ticker,String title, String content, int bigPic, boolean sound, boolean vibrate, boolean lights) {setCompatBuilder(pendingIntent, smallIcon, ticker, title, null, sound, vibrate, lights);NotificationCompat.BigPictureStyle picStyle = new NotificationCompat.BigPictureStyle();final BitmapFactory.Options options = new BitmapFactory.Options();options.inScaled = true;options.inSampleSize = 2;Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),bigPic, options);picStyle.bigPicture(bitmap);picStyle.bigLargeIcon(bitmap);cBuilder.setContentText(content);cBuilder.setStyle(picStyle);sent();}/*** 里面有两个按钮的通知** @param smallIcon* @param leftbtnicon* @param lefttext* @param leftPendIntent* @param rightbtnicon* @param righttext* @param rightPendIntent* @param ticker* @param title* @param content*/public void notifyButton(int smallIcon, int leftbtnicon, String lefttext, PendingIntent leftPendIntent, int rightbtnicon, String righttext, PendingIntent rightPendIntent, String ticker,String title, String content, boolean sound, boolean vibrate, boolean lights) {setCompatBuilder(rightPendIntent, smallIcon, ticker, title, content, sound, vibrate, lights);cBuilder.addAction(leftbtnicon,lefttext, leftPendIntent);cBuilder.addAction(rightbtnicon,righttext, rightPendIntent);sent();}public void notifyHeadUp(PendingIntent pendingIntent, int smallIcon, int largeIcon,String ticker, String title, String content, int leftbtnicon, String lefttext, PendingIntent leftPendingIntent, int rightbtnicon, String righttext, PendingIntent rightPendingIntent, boolean sound, boolean vibrate, boolean lights) {setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);cBuilder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), largeIcon));if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {cBuilder.addAction(leftbtnicon,lefttext, leftPendingIntent);cBuilder.addAction(rightbtnicon,righttext, rightPendingIntent);} else {Toast.makeText(mContext, "版本低于Andriod5.0,无法体验HeadUp样式通知", Toast.LENGTH_SHORT).show();}sent();}/*** 发送通知*/public void sent() {notification = cBuilder.build();notificationManager.notify(NOTIFICATION_ID, notification);}/*** 根据id清除通知*/public void clear() {notificationManager.cancelAll();}DownLoadListener listener;public void setOnDownLoadListener(DownLoadListener l){listener=l;}public interface DownLoadListener{void OnSuccess(File file);void onFailure(Throwable t, int errorNo, String strMsg);}
}

Android Notification详解相关推荐

  1. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  2. Android Notification 详解

    下图是我对 Notification 做的思维导图,也是本文的主要逻辑.  本文主要讲述 Notification 的基本操作部分,进阶部分的内容还在学习ing~ Notification 概述 N ...

  3. Android Notification 详解(各版本对比)——基本操作

    本篇转载出处:http://www.cnblogs.com/travellife/ 温故而知新,可以为师矣~ 下图是我对 Notification 做的思维导图,也是本文的主要逻辑.  本文主要讲述 ...

  4. Android Notification详解【郭霖公众号推荐】

    目录介绍 1.Notification简单概述 2.Notification通知用途 3.Notification的基本操作 3.1 Notification创建必要的属性 3.2 Notificat ...

  5. android rotate 动画,Android RotateAnimation详解

    RotateAnimation旋转坐标系为以旋转点为坐标系(0,0)点.x轴为0度,顺时针方向旋转一定的角度. 1.RotateAnimation(fromDegrees, toDegrees) [默 ...

  6. Android签名详解(debug和release)

    Android签名详解(debug和release) 1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包不被 ...

  7. 【转】Android菜单详解——理解android中的Menu--不错

    原文网址:http://www.cnblogs.com/qingblog/archive/2012/06/08/2541709.html 前言 今天看了pro android 3中menu这一章,对A ...

  8. Android菜单详解——理解android中的Menu

    前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至 ...

  9. Android LayoutInflater详解

    Android LayoutInflater详解 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类 似于findViewById().不同点是LayoutInflater是用来 ...

最新文章

  1. script标签的defer属性
  2. 在VMware Workstation中安装Ubuntu设置网络连接
  3. listview 打开文件 c#_.NET CORE(C#) WPF简单菜单MVVM绑定
  4. Druid 分析报表中的实战(二)
  5. 《操作系统》OS学习(二):启动、中断、异常
  6. 90后美女大学生,年薪30W的程序员,他们都决定去送外卖了!
  7. prototype.js教程及prototype中文手册
  8. 微课|中学生可以这样学Python(5.1.3节):列表常用方法
  9. oracle的.aud文件,Oracle 11g 在audit_file_dest目录下产生大量的aud文件
  10. 笨方法学python 习题41
  11. coreldraw怎样定数等分_cdr怎样将一个圆形平均划分为三等分?
  12. 拼图游戏代码html5,翻译的HTML5拼图游戏(附源码)
  13. eclipse将安卓项目commit至github本地仓库后,项目bin文件夹下不生成apk且运行报错
  14. 文献阅读---对β-三酮除草剂具有广谱抗性的一个水稻基因研究
  15. 基于SPI协议的Flash驱动控制-扇区擦除
  16. 12个最应该使用的Linux服务器OS(上)
  17. 【举例说明】Word中如何自动生成目录以及设置格式
  18. 8个神奇有趣的网站推荐
  19. Emqtt -- 03 -- 用户密码认证
  20. 【精华】PMP认证最全介绍!

热门文章

  1. C++数据结构与算法之二叉树中序遍历
  2. 5.1 Photoshop创建图层的几种方式 [原创Ps教程]
  3. 微信JSSDK分享接口教程,wechat,share ,onMenuShareAppMessage wx.onMenuShareTimeline
  4. cocos2d-x iphone真机测试出现闪屏现象
  5. Adobe Photoshop 2020(PS2020)打开闪退完美解决方案
  6. 求教 | SQL的count()函数里居然能加两个参数
  7. 《短视频,内容设计+营销推广+流量变现》---向登付出,读书笔记
  8. 【基础篇】MySQL
  9. spring boot项目中处理Schedule定时任务
  10. 是男人就撑100秒自制版(VB)