android 下载应用 通知栏显示进度 下完之后点击安装 (很实用)
来源:互联网 发布:剑侠情缘挂机软件 编辑:程序博客网 时间:2024/06/08 06:05
先看效果图: 这是本人的习惯,先上图显示效果,看是否是想要的,再看代码。有图有真相
代码:
Main:
package com.gem.hsx.appupdate;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class Main extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button btnok=(Button) findViewById(R.id.btnok);btnok.setOnClickListener(new BtnokOnClickListener());}private class BtnokOnClickListener implements OnClickListener {@Overridepublic void onClick(View v) {Intent updateIntent =new Intent(Main.this, UpdateService.class);updateIntent.putExtra("titleId",R.string.app_name);startService(updateIntent);}}}
UpdateService:
package com.gem.hsx.appupdate;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.net.Uri;import android.os.Environment;import android.os.Handler;import android.os.IBinder;import android.os.Message;public class UpdateService extends Service{//标题private int titleId = 0;private final static int DOWNLOAD_COMPLETE = 0;private final static int DOWNLOAD_FAIL = 1; //文件存储private File updateDir = null;private File updateFile = null; //通知栏private NotificationManager updateNotificationManager = null;private Notification updateNotification = null;//通知栏跳转Intentprivate Intent updateIntent = null;private PendingIntent updatePendingIntent = null;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {//获取传值 titleId = intent.getIntExtra("titleId",0); //创建文件 if(android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())){ updateDir = new File(Environment.getExternalStorageDirectory(),"app/download/"); updateFile = new File(updateDir.getPath(),getResources().getString(titleId)+".apk"); } this.updateNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); this.updateNotification = new Notification(); //设置下载过程中,点击通知栏,回到主界面 updateIntent = new Intent(this, Main.class); updatePendingIntent = PendingIntent.getActivity(this,0,updateIntent,0); //设置通知栏显示内容 updateNotification.icon = R.drawable.ic_launcher; updateNotification.tickerText = "开始下载"; updateNotification.setLatestEventInfo(this,"上海地铁","0%",updatePendingIntent); //发出通知 updateNotificationManager.notify(0,updateNotification); //开启一个新的线程下载,如果使用Service同步下载,会导致ANR问题,Service本身也会阻塞 new Thread(new updateRunnable()).start();//这个是下载的重点,是下载的过程 return super.onStartCommand(intent, flags, startId);}private Handler updateHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch(msg.what){ case DOWNLOAD_COMPLETE: updateNotification.flags|=updateNotification.FLAG_AUTO_CANCEL; //点击安装PendingIntent Uri uri = Uri.fromFile(updateFile); Intent installIntent = new Intent(Intent.ACTION_VIEW); installIntent.setDataAndType(uri, "application/vnd.android.package-archive"); updatePendingIntent = PendingIntent.getActivity(UpdateService.this, 0, installIntent, 0); updateNotification.defaults = Notification.DEFAULT_SOUND;//铃声提醒 updateNotification.setLatestEventInfo(UpdateService.this, "上海地铁", "下载完成,点击安装。", updatePendingIntent); updateNotificationManager.notify(0, updateNotification); //停止服务 stopService(updateIntent); case DOWNLOAD_FAIL: //下载失败 updateNotification.setLatestEventInfo(UpdateService.this, "上海地铁", "下载完成,点击安装。", updatePendingIntent); updateNotificationManager.notify(0, updateNotification); default: stopService(updateIntent); } }};class updateRunnable implements Runnable { Message message = updateHandler.obtainMessage(); public void run() { message.what = DOWNLOAD_COMPLETE; try{ //增加权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">; if(!updateDir.exists()){ updateDir.mkdirs(); } if(!updateFile.exists()){ updateFile.createNewFile(); } //下载函数,以QQ为例子 //增加权限<uses-permission android:name="android.permission.INTERNET">; long downloadSize = downloadUpdateFile("http://softfile.3g.qq.com:8080/msoft/179/1105/10753/MobileQQ1.0(Android)_Build0198.apk",updateFile); if(downloadSize>0){ //下载成功 updateHandler.sendMessage(message); } }catch(Exception ex){ ex.printStackTrace(); message.what = DOWNLOAD_FAIL; //下载失败 updateHandler.sendMessage(message); } } }public long downloadUpdateFile(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("fail!"); } is = httpConnection.getInputStream(); fos = new FileOutputStream(saveFile, false); byte buffer[] = new byte[4096]; int readsize = 0; while((readsize = is.read(buffer)) > 0){ fos.write(buffer, 0, readsize); totalSize += readsize; //为了防止频繁的通知导致应用吃紧,百分比增加10才通知一次 if((downloadCount == 0)||(int) (totalSize*100/updateTotalSize)-10>downloadCount){ downloadCount += 10; updateNotification.setLatestEventInfo(UpdateService.this, "正在下载", (int)totalSize*100/updateTotalSize+"%", updatePendingIntent); updateNotificationManager.notify(0, updateNotification); } } } finally { if(httpConnection != null) { httpConnection.disconnect(); } if(is != null) { is.close(); } if(fos != null) { fos.close(); } } return totalSize; }}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gem.hsx.appupdate" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".Main" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="UpdateService" android:label="@string/app_name" > </service> </application></manifest>
- android 下载应用 通知栏显示进度 下完之后点击安装 (很实用)
- android+https网址屏蔽证书+IntentService+NotifyManager通知栏+下载apk文件+在通知栏显示下载进度+完成之后提示安装
- android 通知栏显示下载进度
- android第三更(下载附件,通知栏显示进度)
- 通知栏显示下载进度
- NotificationDownLoad 通知栏显示下载进度
- android文件下载及自定义通知显示下载进度
- android文件下载及自定义通知显示下载进度
- Android 实现应用下载并在通知栏展示进度通知 打开APK文件
- Android开发之文件下载,状态时显示下载进度,点击自动安装
- Android开发之文件下载,状态时显示下载进度,点击自动安装
- Android下载,在通知栏更新进度
- Android——下载apk文件,并在通知栏显示下载进度
- Android 自动更新 后代通知栏显示进度
- android实现下载图片在(Notification)通知栏上显示进度。
- 通过服务来下载APK,并在通知栏显示下载进度(两种方式)
- 关于使用Android installer安装应用,安装好之后点击“打开”按钮进入应用之后的界面显示问题
- 关于使用Android installer安装应用,安装好之后点击“打开”按钮进入应用之后的界面显示问题
- linux插件安装一些
- C#日期处理
- 在struts2下不刷新上传excel文件,并回调页面方法!
- js||jquery清空上传框
- Extjs4 model的创建,实例化,自定义方法及取值
- android 下载应用 通知栏显示进度 下完之后点击安装 (很实用)
- java 读excel
- Android通过tcpdump抓包
- 10大iOS开发者最爱的库
- 初窥函数指针
- oracle 物化视图介绍
- KVM 虚拟机在物理主机之间迁移的实现
- NSDateFormatter的使用
- 【phpcms-v9】登陆页、注册页、会员中心页logo图的替换