Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android實現程序自動升級到安裝示例分享(下載android程序安裝包)

android實現程序自動升級到安裝示例分享(下載android程序安裝包)

編輯:關於Android編程

復制代碼 代碼如下:
//程序下載升級 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();
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved