package com.ycb.www.update;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.widget.RemoteViews;
import java.io.File;
/**
* notification更新的广播接收者,根据action不同,做出的结果不同,
* 其中intent因为是同一个intent的,所以并没有new 新的
*
*/
public class UpdateReceiver extends BroadcastReceiver {
private NotificationManager manager;
private RemoteViews views;
private Notification notification;
@Override
public void onReceive(Context context, Intent intent) {
if(notification==null) {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN)
initNotification(context);
else{
initNotificationForLowVersion(context);
}
}
String action = intent.getAction();
switch (action) {
case "com.ycb.www.cancel":
manager.cancel(0);
UpdateService.downLoadHandler.cancel();
break;
case "com.ycb.www.failed":
intent.setAction("com.ycb.www.restart");
PendingIntent failedpendingIntent=PendingIntent.getBroadcast(context, 200, intent, PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.ll_content, failedpendingIntent);
views.setTextViewText(R.id.tv_info, "下载失败,点击重试");
manager.notify(0, notification);
break;
case "com.ycb.www.restart":
manager.cancel(0);
intent.setClass(context, UpdateService.class);
context.startService(intent);
break;
case "com.ycb.www.install":
manager.cancel(0);
Intent startInstall =new Intent();
startInstall.setAction(Intent.ACTION_VIEW);
String filepath = intent.getStringExtra("filepath");
startInstall.setDataAndType(Uri.fromFile(new File(filepath)), "application/vnd.android.package-archive");
startInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startInstall);
break;
case "com.ycb.www.complete":
intent.setAction("com.ycb.www.install");
PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 200, intent, PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.ll_content, pendingIntent);
views.setTextViewText(R.id.tv_info, "下载完成,点击安装");
views.setProgressBar(R.id.progressBar, 100, 100, false);
manager.notify(0, notification);
break;
case "com.ycb.www.updating":
int rate = intent.getIntExtra("rate", 0);
views.setTextViewText(R.id.tv_info,"正在下载...."+rate+"%");
views.setProgressBar(R.id.progressBar,100,rate,false);
manager.notify(0, notification);
}
}
private void initNotificationForLowVersion(Context context) {
//设置notifiction布局
views = new RemoteViews(context.getPackageName(), R.layout.notification_update);
notification=new Notification();
notification.when=System.currentTimeMillis();
notification.tickerText="xxxx新版正在下载";
//设置view
notification.contentView=views;
//设置小图标
notification.icon=R.drawable.ic_launcher;
//设置布局文件中的textView的内容
views.setTextViewText(R.id.tv_info, "下载中....0%");
//设置布局文件中的ProgressBar进度
views.setProgressBar(R.id.progressBar, 100, 0, false);
//退出的intent
Intent intent=new Intent("com.ycb.www.cancel");
//退出的延迟意图
PendingIntent mPendingIntent =PendingIntent.getBroadcast(context.getApplicationContext(),200,intent,PendingIntent.FLAG_UPDATE_CURRENT);
//点击之后退出
views.setOnClickPendingIntent(R.id.ib_close, mPendingIntent);
}
/**
* 初始化notification
* @param context
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void initNotification(Context context){
manager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
views = new RemoteViews(context.getPackageName(), R.layout.notification_update);
Notification.Builder builder=new Notification.Builder(context.getApplicationContext());
notification = builder.setAutoCancel(false).setSmallIcon(R.drawable.ic_launcher).setContentText("下载中").setContentTitle("下载").
setWhen(System.currentTimeMillis()).setTicker("xxxxx新版正在下载")
.setContent(views).build();
views.setTextViewText(R.id.tv_info, "下载中....0%");
views.setProgressBar(R.id.progressBar, 100, 0, false);
Intent intent=new Intent("com.ycb.www.cancel");
PendingIntent mPendingIntent =PendingIntent.getBroadcast(context.getApplicationContext(),200,intent,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.ib_close, mPendingIntent);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android notification+Service实时更新
共22个文件
xml:8个
png:6个
java:4个
5星 · 超过95%的资源 需积分: 50 240 下载量 183 浏览量
2015-12-21
13:44:02
上传
评论 1
收藏 42KB RAR 举报
温馨提示
主要利用了notification service 和receiver 实现了用户通知栏更新,失败了点击重新下载,下载好了点击自动安装,布局很丑,请根据自己项目的实际需求自行修改
资源推荐
资源详情
资源评论
收起资源包目录
update.rar (22个子文件)
update
proguard-rules.pro 657B
update.iml 7KB
src
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
drawable-hdpi
ic_launcher.png 3KB
drawable
mipmap-hdpi
ic_launcher.png 3KB
values
colors.xml 214B
strings.xml 72B
styles.xml 394B
dimens.xml 216B
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
layout
activity_main.xml 714B
notification_update.xml 1KB
values-w820dp
dimens.xml 364B
java
com
ycb
www
update
App.java 285B
MainActivity.java 999B
UpdateReceiver.java 6KB
UpdateService.java 4KB
AndroidManifest.xml 2KB
.gitignore 8B
build.gradle 685B
共 22 条
- 1
资源评论
- zhanshen6342018-12-13很好的资源
- jz_行者2016-10-09正常运行,支持一下!
- pslin2017-04-17感谢分享!!
- chenqi199001302017-10-20很好的资源 值得学习 感谢分享
tom黄
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功