有时候会有些业务需要后台运行并以通知的形式,比如升级,监控什么的。
这里说下我的业务,通过提示用户升级,然后点击升级开启一个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);
}
}
};