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

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

複製程式碼 程式碼如下:
//程式下載升級 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 App實現應用內部自動更新的最基本方法示例Android程式設計實現應用自動更新、下載、安裝的方法Android 軟體自動更新功能實現的方法android自動安裝apk程式碼例項(不使用apk安裝器安裝)Android程式自動更新功能模組的實現方法【附完整demo原始碼下載】

Android 開發 最新文章