【发布时间】:2021-01-28 19:43:45
【问题描述】:
我在 play store 中发布了一个版本代码为 3 的应用,在新版本中,我想实现应用内更新功能,所以在 build.gradle 中添加:
implementation 'com.google.android.play:core:1.8.2'
在主要活动中:
public class MainActivity extends BaseActivity {
//...
private AppUpdateManager mAppUpdateManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//....
checkForUpdate();
}
/**
* check for update
*/
private void checkForUpdate() {
// Creates instance of the manager.
mAppUpdateManager = AppUpdateManagerFactory.create(this);
mAppUpdateManager.registerListener(state -> {
if (state.installStatus() == InstallStatus.DOWNLOADED) {
// After the update is downloaded, show a notification
// and request user confirmation to restart the app.
popupSnackbarForCompleteUpdate();
}
});
// Returns an intent object that you use to check for an update.
Task<AppUpdateInfo> appUpdateInfoTask = mAppUpdateManager.getAppUpdateInfo();
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
//&& appUpdateInfo.clientVersionStalenessDays() != null
//&& appUpdateInfo.clientVersionStalenessDays() >= DAYS_FOR_FLEXIBLE_UPDATE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
// Request the update.
try {
mAppUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,
// Or 'AppUpdateType.IMMEDIATE' for flexible updates.
AppUpdateType.FLEXIBLE,
// The current activity making the update request.
this,
// Include a request code to later monitor this update request.
MY_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
Toast.makeText(this, R.string.update_failed, Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* Displays the snackbar notification and call to action.
*/
private void popupSnackbarForCompleteUpdate() {
final Snackbar snackbar =
Snackbar.make(
findViewById(R.id.activity_main_layout),
R.string.an_update_has_been_just_downloaded,
Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(R.string.install, view -> mAppUpdateManager.completeUpdate());
snackbar.setActionTextColor(
ContextCompat.getColor(this, R.color.color_snackbar_action_text_color));
snackbar.show();
}
}
为了能够测试此功能,我将 versionCode 更改为低于 Playstore 中发布的版本代码(在本例中为 2,应用发布为 3)当我运行我的应用时,它会向我显示对话框更新应用程序,当我单击更新按钮时,更新开始并安装没有任何错误,但是当应用程序重新启动时,它再次显示更新对话框并且应用程序似乎没有更新,我不知道这是不是我的应用程序中的错误,或者我应该将应用程序上传到 Play 商店以正常工作。
我正在使用应用程序包 (.aab)
【问题讨论】:
-
在哪里检查这个版本代码比下载的低??
-
@Ali Vatanparast 为我检查 play 核心库
标签: android in-app-update google-play-core