In App Update Without Playstore
Step1:Check Versions
int versionCode = BuildConfig.VERSION_CODE;
String versionName = BuildConfig.VERSION_NAME;
int oldVersionCode = 5;
Step2: Compare Versions
if(oldVersionCode < versionCode){
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("App Update!!!!!");
dialogBuilder.setMessage("minor changes");
dialogBuilder.setCancelable(false);
dialogBuilder.setPositiveButton("Update", (dialog, which) -> {
if(ActivityCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}else {
downloadFiles();
}
});
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}
Step3: Downloading APK file using Download Manager
void downloadFiles(){
File folder = new File(Environment.getExternalStorageDirectory() + "/" + "SASA");
folder.mkdir();
File file = new File(folder, "AppName.apk");
if (file.exists())
//file.delete() - test this, I think sometimes it doesnt work
file.delete();
//get url of app on server
String url = "https://androidwave.com/source/apk/app-pagination-recyclerview.apk";
Log.d("TAG", "downloadFiles: "+file+" des ");
//set downloadmanager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading");
request.setTitle(Splash.this.getString(R.string.app_name));
//set destination
request.setDestinationUri(Uri.fromFile(file));
// get download service and enqueue file
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
//set BroadcastReceiver to install app when .apk is downloaded
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(Uri.fromFile(file),
manager.getMimeTypeForDownloadedFile(downloadId));
startActivity(install);
unregisterReceiver(this);
finish();
}
};
//register receiver for when .apk download is compete
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
Note:This Code working up to android version Pie( in android 10(Q) getting errors)