我尝试了以下 FLEXIBLE 应用内更新android的代码,并在对话框中单击“更新”时,它开始在后台从Play商店下载新更新,我添加了侦听器为 InstallStateUpdatedListener ,但下载完成后它不会通知我,还检查了 onActivityResult()但不起作用。
后台下载完成后如何获得通知?
AppUpdateManager appUpdateManager;
int UPDATE_REQUEST_CODE = 1;
private void updateTheApp() {
appUpdateManager = AppUpdateManagerFactory.create(this);
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
appUpdateInfoTask.addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() {
@Override
public void onSuccess(AppUpdateInfo appUpdateInfo) {
Log.e("AVAILABLE_VERSION_CODE", appUpdateInfo.availableVersionCode());
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
try {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
MainActivity.this,
UPDATE_REQUEST_CODE);
} catch (IntentSender.SendIntentException ignored) {
}
}
}
});
appUpdateManager.registerListener(installStateUpdatedListener);
appUpdateManager.unregisterListener(installStateUpdatedListener);
}
答案 0 :(得分:0)
appUpdateManager.registerListener(installStateUpdatedListener);
appUpdateManager.unregisterListener(installStateUpdatedListener);
注册后立即删除监听器
答案 1 :(得分:0)
创建一个可以监听后台更新的监听器,
在<meta name="viewport" content="width=device-width, initial-scale=1.0">
进入state.installStatus()
状态后,我们还必须将更新标记为完成。
InstallStatus.DOWNLOADED
创建
的实例后,您可以在onCreate()中请求InAppUpdate。InstallStateUpdatedListener listener = new InstallStateUpdatedListener() {
@Override
public void onStateUpdate(InstallState state) {
Log.d("installState", state.toString());
if (state.installStatus() == InstallStatus.DOWNLOADED) {
// After the update is downloaded, show a notification
// and request user confirmation to restart the app.
popupSnackbarForCompleteUpdate();
}
}
};
您可以注册侦听器。
popupSnackbarForCompleteUpdate()方法,
appUpdateManger = AppUpdateManagerFactory.create(context);
完成这项工作后,您还必须注销监听器。
private void popupSnackbarForCompleteUpdate() {
Snackbar snackbar =
Snackbar.make(findViewById(android.R.id.content), "An update has just been downloaded.", Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("Restart", view -> appUpdateManager.completeUpdate());
snackbar.setActionTextColor(getResources().getColor(android.R.color.white));
snackbar.show();
}