我正在我的android应用程序中实现应用程序内更新功能,我不知道这个概念。谁能给我提供例子。我的要求是,每当用户在登录后单击更新应用程序的按钮时,它都必须显示一个弹出窗口,说明更新是否可用。如果可用,请继续进行更新。 TIA
答案 0 :(得分:1)
Google现在为此提供了官方解决方案
使用AppUpdateManager
检查更新可用性
在请求更新之前,您需要首先检查一个 适用于您的应用。要检查更新,请使用AppUpdateManager, 如下所示:
// Creates instance of the manager. AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(context); // Returns an intent object that you use to check for an update. Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo(); // 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. } });
结果包含更新可用性状态。如果更新是 可用并且允许更新,还返回了AppUpdateInfo 包含开始更新的意图。请参阅下一部分,了解如何 开始更新。
如果应用内更新已在进行中,结果也将 报告正在进行的更新的状态。
有关完整过程,请阅读in-app-updates
答案 1 :(得分:0)
处理更新应用程序弹出对话框的最佳方法,您应该在fire base中进行远程配置。首先向您的应用程序添加Firebase要求,然后将远程配置库添加到依赖项。您可以在Firebase控制台中定义键和值,并在您的启动活动或类似操作中获取它。使用从远程配置中获取的该值检查版本名称应用程序,并比较两个版本。如果两个字符串不相等,则应显示更新应用程序的对话框。
答案 2 :(得分:0)
最近我实现了Google最新的In App Update FLEXIBLE类型,它非常酷。在这里,我添加了按钮onClickListener,它告诉更新是否可用,并且必须是api 21及更高版本,否则它将无法正常工作。
private AppUpdateManager appUpdateManager;
private static final int MY_REQUEST_CODE = 17326;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkUpdate();
}
});
}
private void checkUpdate(){
appUpdateManager = AppUpdateManagerFactory.create(this);
appUpdateManager.registerListener(listener);
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
appUpdateInfoTask.addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() {
@Override
public void onSuccess(AppUpdateInfo appUpdateInfo) {
Log.d("appUpdateInfo :", "packageName :"+appUpdateInfo.packageName()+ ", "+ "availableVersionCode :"+ appUpdateInfo.availableVersionCode() +", "+"updateAvailability :"+ appUpdateInfo.updateAvailability() +", "+ "installStatus :" + appUpdateInfo.installStatus() );
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(FLEXIBLE)){
requestUpdate(appUpdateInfo);
Log.d("UpdateAvailable","update is there ");
}
else if (appUpdateInfo.updateAvailability() == 3){
Log.d("Update","3");
notifyUser();
}
else
{
Toast.makeText(MainActivity.this, "No Update Available", Toast.LENGTH_SHORT).show();
Log.d("NoUpdateAvailable","update is not there ");
}
}
});
}
private void requestUpdate(AppUpdateInfo appUpdateInfo){
try {
appUpdateManager.startUpdateFlowForResult(appUpdateInfo,AppUpdateType.FLEXIBLE,MainActivity.this,MY_REQUEST_CODE);
resume();
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MY_REQUEST_CODE){
switch (resultCode){
case Activity.RESULT_OK:
if(resultCode != RESULT_OK){
Toast.makeText(this,"RESULT_OK" +resultCode, Toast.LENGTH_LONG).show();
Log.d("RESULT_OK :",""+resultCode);
}
break;
case Activity.RESULT_CANCELED:
if (resultCode != RESULT_CANCELED){
Toast.makeText(this,"RESULT_CANCELED" +resultCode, Toast.LENGTH_LONG).show();
Log.d("RESULT_CANCELED :",""+resultCode);
}
break;
case ActivityResult.RESULT_IN_APP_UPDATE_FAILED:
if (resultCode != RESULT_IN_APP_UPDATE_FAILED){
Toast.makeText(this,"RESULT_IN_APP_UPDATE_FAILED" +resultCode, Toast.LENGTH_LONG).show();
Log.d("RESULT_IN_APP_FAILED:",""+resultCode);
}
}
}
}
InstallStateUpdatedListener listener = new InstallStateUpdatedListener() {
@Override
public void onStateUpdate(InstallState installState) {
if (installState.installStatus() == InstallStatus.DOWNLOADED){
Log.d("InstallDownloded","InstallStatus sucsses");
notifyUser();
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
appUpdateManager.unregisterListener((InstallStateUpdatedListener) this);
}
private void notifyUser() {
Snackbar snackbar =
Snackbar.make(findViewById(R.id.message),
"An update has just been downloaded.",
Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("RESTART", new View.OnClickListener() {
@Override
public void onClick(View view) {
appUpdateManager.completeUpdate();
}
});
snackbar.setActionTextColor(
getResources().getColor(R.color.snackbar_action_text_color));
snackbar.show();
}
private void resume(){
appUpdateManager.getAppUpdateInfo().addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() {
@Override
public void onSuccess(AppUpdateInfo appUpdateInfo) {
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED){
notifyUser();
}
}
});
}