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

所属分类: 软件编程 / Android 阅读数: 1329
收藏 0 赞 0 分享

复制代码 代码如下:

//程序下载升级 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手机中很常见的一个实用功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android实现Activity界面切换添加动画特效的方法

这篇文章主要介绍了Android实现Activity界面切换添加动画特效的方法,非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中Dialog去黑边的方法

这篇文章主要介绍了Android中Dialog去黑边的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt for Android开发实例教程

这篇文章主要介绍了Qt for Android开发的方法,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期操作实例

这篇文章主要介绍了Android开发之时间日期操作,是Android程序开发中常见的一个功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期组件用法实例

这篇文章主要介绍了Android开发之时间日期组件用法,主要介绍了TimePicker和DatePicker组件,对于Android程序开发有不错的借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之获取网络链接状态

这篇文章主要介绍了Android获取网络链接状态的方法,主要是通过ConnectivityManager类来完成的,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之广播机制浅析

这篇文章主要介绍了Android开发之广播机制浅析,主要包括了发布、接收及配置广播的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之登录验证实例教程

这篇文章主要介绍了Android开发之登录验证实现方法,包括发送数据、服务器端验证、配置文件等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之注册登录方法示例

这篇文章主要介绍了Android开发的注册登录方法,是针对Android程序设计中版本兼容性的进一步完善,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多