Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android apk版本更新

android apk版本更新

編輯:關於Android編程

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();
}

}

 

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

 

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