您的位置:首页 > 移动开发 > Android开发

Android客户端apk自动检测更新自动下载自动安装的实现方法

2016-03-14 22:03 1581 查看
改进了一个可以检测版本更新自动下载自动安装的客户端升级方案。

在下载之前删除之前的历史下载文件,减少垃圾数据。

先给出核心类

public class DownloadService extends Service {
private DownloadManager mDownloadManager;
private long enqueue;
private BroadcastReceiver receiver;
private  static final String APK_URL= IPAddress.DEFAULT_IP+"/portrait/app-youni.apk";
//    private  static final String APK_NAME="youni.apk";
private  static final String APK_NAME="youni_"+ System.currentTimeMillis()+"_.apk";

@Nullable
@Override
public IBinder onBind(Intent intent) {
DebugLog.v("onBind");
return null;
}
@Override
public void onCreate() {
DebugLog.v("onCreate");
super.onCreate();
/*删除以前下载的安装包*/
RecursionDeleteFile(new File(Environment.getExternalStorageDirectory() + "/download/youni/"));
}

@Override
public void onStart(Intent intent, int startId) {
DebugLog.v("onStart");
super.onStart(intent, startId);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
DebugLog.v("onStartCommand");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/youni/"+APK_NAME)),
"application/vnd.android.package-archive");
startActivity(intent);
stopSelf();
}
};
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
startDownload();
return Service.START_STICKY;
}

@Override
public void onDestroy() {
DebugLog.v("onDestroy");
unregisterReceiver(receiver);
super.onDestroy();
}

private void startDownload() {
mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(APK_URL));
request.setDescription("正在为您下载 友你 App的最新版本");
request.setMimeType("application/vnd.android.package-archive");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS+"/youni", APK_NAME);
enqueue = mDownloadManager.enqueue(request);
}
/**
* 递归删除文件和文件夹
* @param file    要删除的根目录
*/
public void RecursionDeleteFile(File file){
if(file.isFile()){
file.delete();
return;
}
if(file.isDirectory()){
File[] childFile = file.listFiles();
if(childFile == null || childFile.length == 0){
file.delete();
return;
}
for(File f : childFile){
RecursionDeleteFile(f);
}
file.delete();
}
}
}

然后再检测到服务器有新版本后,弹出对话框,询问用户是否下载

/*系统提示对话框*/
new AlertDialog.Builder(NewMainActivity.this).setTitle("操作提示")//设置对话框标题
.setMessage("检测到最新版本,是否要升级?")//设置显示的内容
.setPositiveButton("后台下载",new DialogInterface.OnClickListener() {//添加确定按钮
@Override
public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
startService(new Intent(getApplicationContext(), DownloadService.class));
}
}).setNegativeButton("下次再说",new DialogInterface.OnClickListener() {//添加返回按钮
@Override
public void onClick(DialogInterface dialog, int which) {//响应事件

}
}).show();//在按键响应事件中显示此对话框

下载后就可以自动安装了





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: