I have an Android app that is private, and NOT on Google market or Google play. The apk file is hosted on a private web server, and there is also a URL that the mobile device can do a POST request so that it checks if there is a newer version.
Right now, if there is a newer version, I just show a button to the user that says "Upgrade". An onclick listener does this:
final View.OnClickListener upgradeButton_OnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "https://example.com/android-app/app-release.apk";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
};
... but this isn't ideal. It would be nice if when I clicked the button that the apk file is downloaded and I am brought to the app installer screen where it says "Install yes/no". I just don't know how to do that.
I've been all over the internet looking for a way to do this, and I've not seen a conclusive answer. Can somebody please point me to a tutorial with all the steps? I know most people probably use Google market or Google play for this, but I'm not doing that, and it's been hard to figure this out.
Edit 1 -----------------------------------------
Now I have this in onclick listener:
final View.OnClickListener upgradeButton_OnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permissionCheck == PackageManager.PERMISSION_GRANTED){
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://example.com/android-app/app-release.apk"));
request.setTitle("This App Upgrade.");
request.setDescription("File is being downloaded ...");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "app-release.apk" );
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
manager.enqueue(request);
}
}
};
And this:
BroadcastReceiver onDownloadComplete = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
intent.setDataAndType(Uri.parse(Environment.DIRECTORY_DOWNLOADS + "app-release.apk"),
"application/vnd.android.package-archive");
startActivity(intent);
}
};
I was having an error from trying to do the download:
java.lang.SecurityException: No permission to write to /storage/emulated/0/Download/app-release.apk: Neither user 10062 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.
That's why I tried to implement the permissionCheck, but now when I click the button nothing happens at all.