有时候会有些业务需要后台运行并以通知的形式,比如升级,监控什么的。
这里说下我的业务,通过提示用户升级,然后点击升级开启一个service服务在
后台进行下载并以通知的形式提供用户查看,下载完成点击通知进入安装。
1.开启服务
Intent intent = new Intent();
intent.setClass(mContext, UpgradeService.class);
startService(intent);
2.建立一个服务servive类
在onStart方法中建立notification,做写准备工作.
@Override
public void onStart(Intent intent, int startId) {
String sdPath = FileHelper.getSDCardPath();
if (sdPath != null) {
updateFile = new File(sdPath + Global.downloadDir + "petfone.apk");
// 初始化通知管理器
this.updateNotificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
this.updateNotification = new Notification();
updateNotification.icon=R.drawable.ic_launcher;
updateIntent = new Intent(this, UpdateAppDemoActivity.class);
updatePendingIntent = PendingIntent.getActivity(this, 0,updateIntent, 0);
//通知自定义视图
updateNotification.contentView = new RemoteViews(getPackageName(),com.hua.test.R.layout.mynotification_progressbar);
updateNotification.contentView.setProgressBar(com.hua.test.R.id.pb_notifi, 100, 0, false);
updateNotification.contentIntent = updatePendingIntent;//这个pengdingIntent很重要,必须要设置
// 发出通知
//updateNotificationMgr.notify(notificationId, updateNotification);
// 开启线程进行下载
new Thread(new updateThread()).start();
}
super.onStart(intent, startId);
}
3.开启一个线程来下载防止主线程堵塞。这里在servce写了一个内部类实现了Runnable
class updateThread implements Runnable {
Message msg = handler.obtainMessage();
@Override
public void run() {
try {
if (!updateFile.exists()) {
updateFile.createNewFile();
}
long downSize = downloadFile(Global.NET_ADDRESS+"PetFone_G_Google.apk",updateFile);
if(downSize>0){
//下载成功!
msg.what=DOWNLOAD_SUCCESS;
handler.sendMessage(msg);
}
} catch (Exception ex) {
ex.printStackTrace();//下载失败
msg.what=DOWNLOAD_FALL;
handler.sendMessage(msg);
}
}
}
/**
* 下载文件
* @param downloadUrl 下载路径
* @param saveFile 保存文件名
*/
public long downloadFile(String downloadUrl, File saveFile) throws Exception {
int downloadCount = 0;
int currentSize = 0;
long totalSize = 0;
int updateTotalSize = 0;
HttpURLConnection httpConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try{
URL url = new URL(downloadUrl);
httpConnection = (HttpURLConnection)url.openConnection();
httpConnection.setRequestProperty("User-Agent", "PacificHttpClient");
if(currentSize > 0) {
httpConnection.setRequestProperty("RANGE", "bytes=" + currentSize + "-");
}
httpConnection.setConnectTimeout(10000);
httpConnection.setReadTimeout(20000);
updateTotalSize = httpConnection.getContentLength();//总大小
if(httpConnection.getResponseCode()==404){
throw new Exception("conection net 404!");
}
is = httpConnection.getInputStream();
fos = new FileOutputStream(saveFile);
byte[] buf = new byte[1024];
int readSize = -1;
while((readSize = is.read(buf)) != -1){
fos.write(buf, 0, readSize);
//通知更新进度
totalSize += readSize;
int tmp = (int) (totalSize * 100 / updateTotalSize);
//为了防止频繁的通知导致应用吃紧,百分比增加10才通知一次
if(downloadCount == 0 || tmp-10>downloadCount){
downloadCount+=10;
Message msg = handler.obtainMessage();
msg.what=DOWNLOAD_COMPLETE;
msg.arg1=downloadCount;
handler.sendMessage(msg);
}
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
if(httpConnection != null) {
httpConnection.disconnect();
}
if(is != null) {
is.close();
}
if(fos != null) {
fos.close();
}
}
return totalSize;
}
4.需要不段的给progressBar 提供值,所以要使用handler,下载文件一定也就有3种状态,
1).下载中,2).下载失败,3).下载成功。
在servive中定义好handler并定义这个3个状态码。
private final int DOWNLOAD_COMPLETE = 1, DOWNLOAD_FALL=2,DOWNLOAD_SUCCESS=3;
然后在处理这个message
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case DOWNLOAD_SUCCESS:
//下载完成点击通知进入安装
Uri uri = Uri.fromFile(updateFile);
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setDataAndType(uri, "application/vnd.android.package-archive");
updatePendingIntent = PendingIntent.getActivity(UpgradeService.this, 0, installIntent, 0);
updateNotification.defaults = Notification.DEFAULT_SOUND;//设置铃声
updateNotification.contentIntent = updatePendingIntent;
//更新通知视图值
updateNotification.contentView.setTextViewText(com.hua.test.R.id.tv_downInfo, "下载成功,点击安装。");
updateNotification.contentView.setProgressBar(com.hua.test.R.id.pb_notifi, 100, 100, false);
updateNotificationMgr.notify(notificationId, updateNotification);
stopService(updateIntent);//停止service
break;
case DOWNLOAD_COMPLETE://下载中状态
System.out.println(msg.arg1);
updateNotification.contentView.setProgressBar(com.hua.test.R.id.pb_notifi, 100, msg.arg1, false);
updateNotification.contentView.setTextViewText(R.id.tv_downInfo, "下载中"+msg.arg1+"%");
updateNotificationMgr.notify(notificationId, updateNotification);
break;
case DOWNLOAD_FALL://失败状态
//updateNotification.setLatestEventInfo(UpgradeService.this, "下载失败", "", updatePendingIntent);]
updateNotification.contentView.setTextViewText(com.hua.test.R.id.tv_downInfo, "下载失败");
updateNotificationMgr.notify(notificationId, updateNotification);
stopService(updateIntent);//停止service
break;
default:
stopService(updateIntent);
}
}
};
- 大小: 17.2 KB
分享到:
相关推荐
Android一个带有进度条的通知栏的DEMO,对初学Android的初学者是一个不错的参考例子,代码实现了点击显示按钮就会在通知栏上出现一个有图片和进度条的提示,点击取消按钮就会取消通知栏的提示通知。
Android 实现自定义通知栏,并不断更新通知栏的进度条,模拟下载效果。 注:只是模拟下载进度,没有下载功能。
详情:http://blog.csdn.net/onceing/article/details/47317189,如果你觉得不错希望给个评价,谢谢
2套代码,一套通知栏几种方法,另一条显示进度条,导入eclipse直接可以用
本篇文章主要介绍了android项目实现带进度条的系统通知栏消息,就是实现在通知栏看到下载进度。具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
通知栏(自定义、默认、带进度,开始,暂停)
主要介绍了Android编程实现通知栏进度条效果的方法,结合实例形式较为详细的分析了Android通知栏进度条效果的功能、布局相关实现方法与操作注意事项,需要的朋友可以参考下
自定义通知栏布局,自定义进度条背景色,解决在部分手机上通知栏进度不更新的问题
APK下载跟新安装,带通知栏跟新下载进度.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
android中在通知栏中显示进度条的Demo
主要介绍了Android 下载文件通知栏显示进度条功能的实例代码,通过使用AsyncTask异步任务实现,调用publishProgress()方法刷新进度来实现,具体代码大家参考下本
以线程和服务两种方式实现网络下载,发送广播通知进度条和通知栏更新进度。
我们在使用APP的过程中,软件会偶尔提示我们进行版本更新,我们点击确认更新后,会在通知栏显示下载更新进度(已知长度的进度条)以及安装情况(不确定进度条),这就是我们今天要实现的功能。实现效果如下: 在...
主要为大家详细介绍了Android实现带有进度条的按钮效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
软件开发设计:PHP、QT、应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、...
Android各种主流的通知样式,可直接作为工具集成使用,包含:普通通知、进度条通知、多行提示文本通知、自定义视图通知、容纳大图片的通知、有按钮的通知等等