Install Application Programmatically on Android

Install Application programmatically on Android

You can easily launch a market link or an install prompt:

Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("file:///path/to/your.apk"),
"application/vnd.android.package-archive");
startActivity(promptInstall);

source

Intent goToMarket = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("market://details?id=com.package.name"));
startActivity(goToMarket);

source

However, you cannot install .apks without user's explicit permission; not unless the device and your program is rooted.

How to install any Android app programmatically in Android 10

If you want install application programmatically in Android 10, You need to give permission to install app for your application

Steps 1: Give permission in Manifest

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Step 2: Write provider in Android Manifest

 <provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>

Step 4: Create file in XML folder named as provider_paths.xml and write

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="files_root"
path="Android/data/${applicationId}" />
<external-path
name="external_files"
path="." />
</paths>

how to install apk programmatically from another app in android

It seems the problem is that you set wrong uri to intent data:

install.setData(Uri.fromFile(file));

You need to set uri which was created with FileProvider:

Uri uri = FileProvider.getUriForFile(getApplicationContext(), "com.example.myapp", file);
install.setData(uri);

The same is done in the instruction you have found:

val contentUri = FileProvider.getUriForFile(
context,
BuildConfig.APPLICATION_ID + PROVIDER_PATH,
File(destination)
)
val install = Intent(Intent.ACTION_VIEW)
install.data = contentUri

How to install application programmatically

Hi I have found somehow the answer.
My code:

  var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW)

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
Permissions.requestPermission(android.Manifest.permission.REQUEST_INSTALL_PACKAGES)

var imagePath = new java.io.File(android.os.Environment.getExternalStorageDirectory(), "Download")
var newFile = new java.io.File(imagePath, "update.apk")
var data = android.support.v4.content.FileProvider.getUriForFile(application.android.context, "org.nativescript.ITPalert.provider", newFile)
intent.setDataAndType(data, "application/vnd.android.package-archive")
intent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
else {
var fileLocation = new java.io.File(application.android.context.getFilesDir(), "Download/update.apk")
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
intent.setDataAndType(android.net.Uri.fromFile(fileLocation), "application/vnd.android.package-archive")
}

application.android.context.startActivity(android.content.Intent.createChooser(intent, "asd"))

In the manifest file I added:

     <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="org.nativescript.ITPalert.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>

Filepath.xml

    <paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="download" path="Download/"/>
</paths>

Android: install .apk programmatically

I solved the problem. I made mistake in setData(Uri) and setType(String).

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

That is correct now, my auto-update is working. Thanks for help. =)

Edit 20.7.2016:

After a long time, I had to use this way of updating again in another project. I encountered a number of problems with old solution. A lot of things have changed in that time, so I had to do this with a different approach. Here is the code:

    //get destination to update file and set Uri
//TODO: First I wanted to store my update .apk file on internal storage for my app but apparently android does not allow you to open and install
//aplication with existing package from there. So for me, alternative solution is Download directory in external storage. If there is better
//solution, please inform us in comment
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "AppName.apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);

//Delete update file if exists
File file = new File(destination);
if (file.exists())
//file.delete() - test this, I think sometimes it doesnt work
file.delete();

//get url of app on server
String url = Main.this.getString(R.string.update_app_url);

//set downloadmanager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(Main.this.getString(R.string.notification_description));
request.setTitle(Main.this.getString(R.string.app_name));

//set destination
request.setDestinationUri(uri);

// 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) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri,
manager.getMimeTypeForDownloadedFile(downloadId));
startActivity(install);

unregisterReceiver(this);
finish();
}
};
//register receiver for when .apk download is compete
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));


Related Topics



Leave a reply



Submit