android apk版本更新

來源:互聯網
上載者:User

android apk版本更新

android apk版本更新

/**
* 擷取版本名稱
*
* @return version
*/
private String getVersionName() {
try {
// 擷取packagemanager的執行個體
PackageManager packageManager = getPackageManager();
// getPackageName()是你當前類的包名,0代表是擷取版本資訊
PackageInfo packInfo = packageManager.getPackageInfo(
getPackageName(), 0);
version = packInfo.versionName;// versionName是擷取版本名稱還有版本號碼等等...
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return version;
}

activity類調用

// 這裡來檢測版本是否需要更新
UpdateManager mUpdateManager = new UpdateManager(
SettingActivity.this);
mUpdateManager.checkUpdateInfo();

public class UpdateManager {

private Context mContext;
/* 下載包安裝路徑 */
private static final String savePath = "//sdcard//updatedemo//";
private static final String saveFileName = savePath + "licaike.apk";
/* 進度條與通知ui重新整理的handler和msg常量 */

private int progress;
private ProgressDialogUtil pdu;

private boolean interceptFlag = true;

@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x123:
pdu.setProgress(progress);
break;
case 0x124: // 安裝apk
File apkfile = new File(saveFileName);
if (!apkfile.exists()) {
return;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
"application/vnd.android.package-archive");
mContext.startActivity(i);
pdu.dismiss();
break;
default:
break;
}
};
};

public UpdateManager(Context context) {
this.mContext = context;
}

// 外部介面讓主Activity調用
public void checkUpdateInfo() {
final AlertDialogUtil dialogUtil = new AlertDialogUtil(mContext, false,
null);
dialogUtil.setMessage("請更新新版本");
dialogUtil.setBtnPositiveValue("更新");
dialogUtil.setPositiveClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
showDownloadDialog();
dialogUtil.dismiss();
}
});
dialogUtil.setBtnNegativeValue("取消");
dialogUtil.setNegativeClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
dialogUtil.dismiss();
}
});
dialogUtil.show();

}

private void showDownloadDialog() {
// 下載apk
downLoadThread();
pdu = new ProgressDialogUtil(mContext, false, null);
pdu.setMessage("軟體正在更新中...");
pdu.setBtnNegativeValue("取消");
pdu.setNegativeClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
interceptFlag = false;
pdu.dismiss();
}
});
pdu.show();
}

private void downLoadThread() {
new Thread(new Runnable() {

@Override
public void run() {
InputStream is = null;
FileOutputStream fos = null;
try {
// 返回的安裝包url
String apkUrl = "http://d.m.hexun.com/app/licaike.apk";
URL url = new URL(apkUrl);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.connect();
int length = conn.getContentLength();
is = conn.getInputStream();

File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
String apkFile = saveFileName;
File ApkFile = new File(apkFile);
fos = new FileOutputStream(ApkFile);

int count = 0;
byte buf[] = new byte[1024];
while (interceptFlag) {// 點擊取消就停止下載.
int numread = is.read(buf);
count += numread;
progress = (int) (((float) count / length) * 100);
// 更新進度
mHandler.sendEmptyMessage(0x123);
if (numread <= 0) {
// 下載完成通知安裝
mHandler.sendEmptyMessage(0x124);
break;
}
fos.write(buf, 0, numread);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
if (is != null)
is.close();
} catch (Exception e) {
} finally {
fos = null;
is = null;
}
}

}
}).start();
}

}

 

進度條你自己隨便往對話方塊拖個

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.