zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

android实现程序自动升级到安装示例分享(下载android程序安装包)

Android安装下载自动程序 实现 示例 分享
2023-06-13 09:15:16 时间

复制代码代码如下:


//程序下载升级zhouxiang
@JavascriptInterface
publicvoidUpdateCAECP(finalStringpath){
try{
AlertDialog.Builderbuilder=newBuilder((Context)obj);
builder.setMessage(“检测到有新版本发布,是否进行下载升级?”);
builder.setTitle("程序更新提示");
builder.setPositiveButton("升级",newOnClickListener(){
@Override
publicvoidonClick(DialogInterfacearg0,intarg1){
//TODOAuto-generatedmethodstub
m_pDialog=newProgressDialog((Context)obj);
m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
m_pDialog.setTitle("程序升级中");
m_pDialog.setMessage("正在下载最新版的CAECP,请等候…");
m_pDialog.setIcon(R.drawable.ic_launcher);
m_pDialog.setProgress(100);
m_pDialog.setIndeterminate(false);
//设置ProgressDialog是否可以按退回按键取消
m_pDialog.setCancelable(true);
m_pDialog.show();
newCAECP_DownloadFile(m_pDialog,(Context)obj).execute(path);
}
});
builder.setNegativeButton("取消",newOnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,intwhich){
//TODOAuto-generatedmethodstub
dialog.dismiss();
}
});
builder.create().show();
}catch(Exceptione){
Alert("升级提示",e.getMessage(),"确认");
}
}

复制代码代码如下:


//zhouxiang文件下载百分比及自动安装
publicclassCAECP_DownloadFileextendsAsyncTask{
ProgressDialogm_pDialog=null;
Stringpath="/sdcard/caecp/caecp.apk";
staticStringchattemp="/sdcard/caecp/chat.caecp";
staticStringusertemp="/sdcard/caecp/user.caecp";
Contextobj;
CAECP_DownloadFile(ProgressDialogm_pDialog2,Contextobj2){
m_pDialog=m_pDialog2;
obj=obj2;
}
@Override
protectedStringdoInBackground(String…sUrl){
try{
URLurl=newURL(sUrl[0]);
URLConnectionconnection=url.openConnection();
connection.connect();
intfileLength=connection.getContentLength();
InputStreaminput=newBufferedInputStream(url.openStream());
OutputStreamoutput=newFileOutputStream(path);
bytedata[]=newbyte[1024];
longtotal=0;
intcount;
while((count=input.read(data))!=-1){
total+=count;
m_pDialog.setProgress((int)(total*100/fileLength));
output.write(data,0,count);
}
output.flush();
output.close();
input.close();
DownCAECP_Ok();
}catch(Exceptione){
}
returnnull;
}
//下载CAECP文件完成,启动新线程,调用系统进行安装
publicvoidDownCAECP_Ok(){
newThread(){
publicvoidrun(){
Intenti=newIntent(Intent.ACTION_VIEW);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setDataAndType(Uri.parse("file://"+path),"application/vnd.android.package-archive");
obj.startActivity(i);
}
}.start();
}