我正在使用 android in-app-update API,但我没有收到 InstallStatus.INSTALLED 案例的回调。我确实收到了 InstallStatus.DOWNLOADED 案例的回调。而且我也能够成功更新应用程序,但我想在 InstallStatus.INSTALLED 案例中执行一些逻辑。那么我应该如何以及何时回调 InstallStatus.INSTALLED 案例。
我的 InstallStateUpdatedListener
class AppInstallStateUpdatedListener implements InstallStateUpdatedListener {
private AppUpdateManager appUpdateManager;
private View anchorView;
AppInstallStateUpdatedListener(AppUpdateManager appUpdateManager, View anchorView) {
this.appUpdateManager = appUpdateManager;
this.anchorView = anchorView;
}
@Override
public void onStateUpdate(InstallState state) {
if (state.installStatus() == InstallStatus.DOWNLOADED) {
popupSnackbarForCompleteUpdate(anchorView);
Toast.makeText(context,"Downloaded in app update", Toast.LENGTH_SHORT).show();
} else if (state.installStatus() == InstallStatus.INSTALLED) {
Toast.makeText(context,"Installed in app update", Toast.LENGTH_SHORT).show();
if (appUpdateManager != null) {
appUpdateManager.unregisterListener(this);
}
} else {
Log.i(InAppUpdateUtils.class.getSimpleName(), "InstallStateUpdatedListener: state: " + state.installStatus());
}
}
}
这就是我附加监听器的方式(我有一个类“InAppUpdateUtils”,在它的构造函数中我正在初始化 appUpdateManager 并将监听器附加到它)下面是构造函数。
public InAppUpdateUtils(Activity activity, View anchorView) {
appUpdateManager = AppUpdateManagerFactory.create(activity);
if (listener == null) {
listener = new AppInstallStateUpdatedListener(appUpdateManager, anchorView);
appUpdateManager.registerListener(listener);
}
}
这就是我开始安装的方式
private void popupSnackbarForCompleteUpdate(View anchorVIew) {
Snackbar snackbar =
Snackbar.make(
anchorVIew,
"We have downloaded an update",
Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("INSTALL", v -> {
updateCompleteTask = appUpdateManager.completeUpdate();
if (updateCompleteTask != null) {
updateCompleteTask.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Toast.makeText(QuikrApplication.context, "Install Success", Toast.LENGTH_LONG).show();
Log.d("APP_UPDATE", "Update install success");
}
});
updateCompleteTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(context, "Install Failed", Toast.LENGTH_LONG).show();
Log.d("APP_UPDATE", "Update install failed " + e.toString());
}
});
}
});
TextView tv = snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
snackbar.setActionTextColor(anchorVIew.getContext().getResources().getColor(R.color.green));
snackbar.show();
}