android实现程序自动升级到安装示例分享(下载android程序安装包)
程序员文章站
2022-06-29 09:31:29
复制代码 代码如下://程序下载升级 zhouxiang@javascriptinterfacepublic void updatecaecp(final string p...
复制代码 代码如下:
//程序下载升级 zhouxiang
@javascriptinterface
public void updatecaecp(final string path){
try{
alertdialog.builder builder = new builder((context)obj);
builder.setmessage(“检测到有新版本发布,是否进行下载升级?”);
builder.settitle("程序更新提示");
builder.setpositivebutton("升级", new onclicklistener(){
@override
public void onclick(dialoginterface arg0, int arg1) {
// todo auto-generated method stub
m_pdialog = new progressdialog((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();
new caecp_downloadfile(m_pdialog,(context)obj).execute(path);
}
});
builder.setnegativebutton("取消", new onclicklistener(){
@override
public void onclick(dialoginterface dialog, int which) {
// todo auto-generated method stub
dialog.dismiss();
}
});
builder.create().show();
}catch(exception e){
alert("升级提示", e.getmessage(), "确认");
}
}
复制代码 代码如下:
//zhouxiang 文件下载百分比 及 自动安装
public class caecp_downloadfile extends asynctask{
progressdialog m_pdialog=null;
string path="/sdcard/caecp/caecp.apk";
static string chattemp = "/sdcard/caecp/chat.caecp";
static string usertemp = "/sdcard/caecp/user.caecp";
context obj;
caecp_downloadfile(progressdialog m_pdialog2,context obj2){
m_pdialog=m_pdialog2;
obj=obj2;
}
@override
protected string doinbackground(string… surl) {
try {
url url = new url(surl[0]);
urlconnection connection = url.openconnection();
connection.connect();
int filelength = connection.getcontentlength();
inputstream input = new bufferedinputstream(url.openstream());
outputstream output = new fileoutputstream(path);
byte data[] = new byte[1024];
long total = 0;
int count;
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 (exception e) {
}
return null;
}
//下载caecp文件完成,启动新线程,调用系统进行安装
public void downcaecp_ok(){
new thread(){
public void run() {
intent i = new intent(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();
}
上一篇: 中医养生 女性冬季保健多按摩这些穴位
下一篇: 矩阵乘法(七):其它一些典型应用