手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >android实现程序自动升级到安装示例分享(下载android程序安装包)
android实现程序自动升级到安装示例分享(下载android程序安装包)
摘要:复制代码代码如下://程序下载升级zhouxiang@JavascriptInterfacepublicvoidUpdateCAECP(fi...

复制代码 代码如下:

//程序下载升级 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();

}

【android实现程序自动升级到安装示例分享(下载android程序安装包)】相关文章:

Android UI开发 View自绘控件 分享

Android多线程处理机制中的Handler使用介绍

Android应用程序的调试

android 完全退出应用程序实现代码

Android列表实现(2)_游标列表案例讲解

android通过代码的形式来实现应用程序的安装与卸载

android 权限大全 分享

Android实现原生侧滑菜单的超简单方式

android ListView的右边滚动滑块启用方法 分享

Android:多线程之进程与线程

精品推荐
分类导航