1

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.

Brian Gottier
  • 4,522
  • 3
  • 21
  • 37

1 Answers1

0

Yes, it's possible. With some 'IFs': phone must be rooted and 3rd-party sources installation enabled.

Basically you have to call pm install Android's command once file is downloaded.

This is how you can download the file: Download a file with Android, and showing the progress in a ProgressDialog

And this piece is just an example of pm installed

Process process = null;
try
{
    process  = Runtime.getRuntime().exec("su");
    DataOutputStream dataOutputStream=new DataOutputStream(process.getOutputStream());

    String cmd="pm install /sdcard/LatestUpdate.apk";
    dataOutputStream.writeBytes(cmd+"\n");
}
catch (IOException e)
{
    e.printStackTrace();
}

Without root you may try to invoke a new Intent as

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(filePath)),"application/vnd.android.package-archive");
startActivity(intent);

It will show application installation dialogue. But still Install from Unknown sources is a must

Community
  • 1
  • 1
the.Legend
  • 626
  • 5
  • 13
  • No, the phones can't be rooted. Third party sources I can do. I updated my question to show where I'm at now, which is stuck, but at least stuck differently that I first was stuck. – Brian Gottier Aug 25 '16 at 23:04
  • Add android.permission.WRITE_EXTERNAL_STORAGE to your Android Manifest – the.Legend Aug 26 '16 at 07:07