如何在 android 10 上使用应用内更新来更新应用
How to Update Apps using In-App update on android 10
我正在尝试在 android 上使用应用内更新来更新程序,整个更新步骤在 android 设备上成功完成 android 版本 10Q 除外。在android这个版本上,出现更新对话框,软件更新成功完成,但是没有显示进度条和显示更新成功的页面,程序关闭。我已经用谷歌搜索了几个小时,但没有找到任何解决方案。我的代码有什么问题?
感谢任何帮助。
Create and install status receiver
final String PACKAGE_INSTALLED_ACTION =
"com.example.android.apis.content.SESSION_API_PACKAGE_INSTALLED";
Intent intent = new Intent(ctx, InstallReceiver.class);
intent.setAction(PACKAGE_INSTALLED_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 3439,intent, PendingIntent.FLAG_UPDATE_CURRENT);
session.commit(pendingIntent.getIntentSender());
session.close();
Reciever part
public final class InstallReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra("android.content.pm.extra.STATUS", -1);
if (status == -1) {
Intent activityIntent = intent.getParcelableExtra(Intent.EXTRA_INTENT);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
} else if (status == 0) {
(new ToneGenerator(5, 100)).startTone(25);
} else {
String msg = intent.getStringExtra("android.content.pm.extra.STATUS_MESSAGE");
Log.e("AppInstaller", "received " + status + " and " + msg);
}
}
要进行应用内更新,您必须遵循文档:
https://developer.android.com/guide/playcore/in-app-updates
检查更新是否可用:
// Creates instance of the manager.
val appUpdateManager = AppUpdateManagerFactory.create(context)
// Returns an intent object that you use to check for an update.
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
// For a flexible update, use AppUpdateType.FLEXIBLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
) {
// Request the update.
}
}
开始更新
appUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,
// Or 'AppUpdateType.FLEXIBLE' for flexible updates.
AppUpdateType.IMMEDIATE,
// The current activity making the update request.
this,
// Include a request code to later monitor this update request.
MY_REQUEST_CODE)
更新结果列表:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
if (requestCode == MY_REQUEST_CODE) {
if (resultCode != RESULT_OK) {
log("Update flow failed! Result code: $resultCode")
// If the update is cancelled or fails,
// you can request to start the update again.
}
}
}
我不确定你的代码在做什么,但它没有遵循这个例子。
我认为您的问题可能是检查停滞的更新,因此您应该试试这个:
// Checks that the update is not stalled during 'onResume()'.
// However, you should execute this check at all app entry points.
override fun onResume() {
super.onResume()
appUpdateManager
.appUpdateInfo
.addOnSuccessListener { appUpdateInfo ->
...
// If the update is downloaded but not installed,
// notify the user to complete the update.
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
popupSnackbarForCompleteUpdate()
}
}
}
我正在尝试在 android 上使用应用内更新来更新程序,整个更新步骤在 android 设备上成功完成 android 版本 10Q 除外。在android这个版本上,出现更新对话框,软件更新成功完成,但是没有显示进度条和显示更新成功的页面,程序关闭。我已经用谷歌搜索了几个小时,但没有找到任何解决方案。我的代码有什么问题? 感谢任何帮助。
Create and install status receiver
final String PACKAGE_INSTALLED_ACTION =
"com.example.android.apis.content.SESSION_API_PACKAGE_INSTALLED";
Intent intent = new Intent(ctx, InstallReceiver.class);
intent.setAction(PACKAGE_INSTALLED_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 3439,intent, PendingIntent.FLAG_UPDATE_CURRENT);
session.commit(pendingIntent.getIntentSender());
session.close();
Reciever part
public final class InstallReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra("android.content.pm.extra.STATUS", -1);
if (status == -1) {
Intent activityIntent = intent.getParcelableExtra(Intent.EXTRA_INTENT);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
} else if (status == 0) {
(new ToneGenerator(5, 100)).startTone(25);
} else {
String msg = intent.getStringExtra("android.content.pm.extra.STATUS_MESSAGE");
Log.e("AppInstaller", "received " + status + " and " + msg);
}
}
要进行应用内更新,您必须遵循文档:
https://developer.android.com/guide/playcore/in-app-updates
检查更新是否可用:
// Creates instance of the manager.
val appUpdateManager = AppUpdateManagerFactory.create(context)
// Returns an intent object that you use to check for an update.
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
// For a flexible update, use AppUpdateType.FLEXIBLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
) {
// Request the update.
}
}
开始更新
appUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,
// Or 'AppUpdateType.FLEXIBLE' for flexible updates.
AppUpdateType.IMMEDIATE,
// The current activity making the update request.
this,
// Include a request code to later monitor this update request.
MY_REQUEST_CODE)
更新结果列表:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
if (requestCode == MY_REQUEST_CODE) {
if (resultCode != RESULT_OK) {
log("Update flow failed! Result code: $resultCode")
// If the update is cancelled or fails,
// you can request to start the update again.
}
}
}
我不确定你的代码在做什么,但它没有遵循这个例子。
我认为您的问题可能是检查停滞的更新,因此您应该试试这个:
// Checks that the update is not stalled during 'onResume()'.
// However, you should execute this check at all app entry points.
override fun onResume() {
super.onResume()
appUpdateManager
.appUpdateInfo
.addOnSuccessListener { appUpdateInfo ->
...
// If the update is downloaded but not installed,
// notify the user to complete the update.
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
popupSnackbarForCompleteUpdate()
}
}
}