<p><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">本文采用的是“自定义UI更新”方式</span></p>
首先在AndroidManifest.xml中添加如下配置:
本文采用的是“自定义UI更新”方式
首先在AndroidManifest.xml中添加如下配置:
<meta-data
android:name="BDAPPID"
android:value="123456" />
<meta-data
android:name="BDAPPKEY"
android:value="abcdefghjdksajdla" />
以上2个参数需要到百度手机开发平台申请。
<!-- ↓↓↓ 自更新SDK ↓↓↓ -->
<activity
android:name="com.baidu.autoupdatesdk.ConfirmDialoigActivity"
android:exported="false"
android:screenOrientation="sensor"
android:theme="@style/bdp_update_dialog_style_fullscreen" />
<receiver
android:name="com.baidu.autoupdatesdk.receiver.BDBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.baidu.autoupdatesdk.ACTION_NEW_UPDATE" />
<action android:name="com.baidu.autoupdatesdk.ACTION_DOWNLOAD_COMPLETE" />
<action android:name="com.baidu.autoupdatesdk.ACTION_NEW_AS" />
<action android:name="com.baidu.autoupdatesdk.ACTION_AS_DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
<!-- ↑↑↑ 自更新SDK ↑↑↑ -->
然后在具体代码中实现更新检测及下载更新:
// 检查更新
update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
BDAutoUpdateSDK.cpUpdateCheck(getBaseContext(),
new MyCPCheckUpdateCallback());
}
});
// ---------------------以下为百度助手升级接口------------------------------------------------
private class MyCPCheckUpdateCallback implements CPCheckUpdateCallback {
@Override
public void onCheckUpdateCallback(final AppUpdateInfo info,
AppUpdateInfoForInstall infoForInstall) {
if (infoForInstall != null
&& !TextUtils.isEmpty(infoForInstall.getInstallPath())) {
BDAutoUpdateSDK.cpUpdateInstall(getApplicationContext(),
infoForInstall.getInstallPath());
} else if (info != null) {
Builder builder = new AlertDialog.Builder(HomeSetActivity.this);
builder.setTitle("更新提示")
.setMessage("有新版本,升级到最新版本?")
.setPositiveButton("升级",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
BDAutoUpdateSDK.cpUpdateDownload(
XXXActivity.this, info,
new UpdateDownloadCallback());
}
});
builder.setNegativeButton("取消", null);
builder.create().show();
} else {
Toast.makeText(getApplicationContext(), "当前已是最新版!", 0).show();
}
}
}
private class UpdateDownloadCallback implements CPUpdateDownloadCallback {
@Override
public void onDownloadComplete(String apkPath) {
BDAutoUpdateSDK.cpUpdateInstall(getApplicationContext(), apkPath);
}
@Override
public void onStart() {
}
@Override
public void onPercent(int percent, long rcvLen, long fileSize) {
if (percent % 10 == 0) {
Message msg = new Message();
msg.what = REFRESH_UPDATE_DIAL;
msg.arg1 = percent;
mXXXHandle.sendMessage(msg);
}
}
@Override
public void onFail(Throwable error, String content) {
txt_log.setText(txt_log.getText() + "\n 下载失败: " + content);
mXXXHandle.sendEmptyMessage(DOWNLOAD_APK_FAILE);
}
@Override
public void onStop() {
}
}
// ---------------------------------------------------------------------