dependencies {
implementation 'com.google.android.play:core:1.9.0'
}
public class SplashActivity extends AppCompatActivity {
private final int REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
callInAppUpdate();
}
private void callInAppUpdate(){
// Creates instance of the manager.
final AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(SplashActivity.this);
// Returns an intent object that you use to check for an update.
com.google.android.play.core.tasks.Task appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(AppUpdateInfo appUpdateInfo) {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
// For a flexible update, use AppUpdateType.FLEXIBLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
// Request the update.
try {
appUpdateManager.startUpdateFlowForResult(appUpdateInfo,AppUpdateType.IMMEDIATE, SplashActivity.this,REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
// e.printStackTrace();
Log.d("TAG", "onSuccess: "+ e.getMessage());
}
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null) return;
if (requestCode == REQUEST_CODE) {
Toast.makeText(SplashActivity.this, "Start Download", Toast.LENGTH_SHORT).show();
if (resultCode != RESULT_OK) {
Log.d("tag","Update flow failed! Result code: " + resultCode);
// If the update is cancelled or fails,
// you can request to start the update again.
}
}
}
@Override
protected void onResume() {
super.onResume();
callInAppUpdate();
}
}
0 Comments