简体   繁体   中英

Android install apk programmatically?

I was having some problem when trying to install the apk programmatically and reboot the Android emulator upon installation. I referred to this thread .

Here is my code:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri apkURI = FileProvider.getUriForFile(mActivity, mActivity.getApplicationContext().getPackageName() + ".provider", new File(fullPath));
    intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mActivity.startActivity(intent);

Is there any way to install the apk without starting an intent? Because I am executing the method above in doInBackground() of my AsyncTask. Then in onPostExecute() , I need to show a fragment stating that the installation is successful.

However, with the code above, upon calling the startActivity() it just closed all my fragments.

Unfortunately, you can't install an app completely in the background (assuming that's what you're trying to do by launching the Intent with doInBackground() ) without user intervention. When you launch that intent, you're simply passing off the Intent to the system's package manager and asking it to install it. The package manager will need to ask the user for confirmation. Without root or special privileges, there's no way for you to truly install an APK programmatically in the background even with the android.permission.INSTALL_PACKAGES permission. Hope this answers your question!

Do not forget about runtime permissions

This simple example works for me for API 28. It opens an apk file to install from "Download folder"

For simplification: Download the apk file for application you want to install to "Download" folder of your phone. (There are a lot of instructions to do it programmaticaly, ot you can do it manualy)

To view the example

  • Create xml folder in res folder and create a file_paths.xml file there
  • Use the code bellow

Manifest

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

    <application
        ...

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

MainActivity Call ActivityCompat.requestPermissions(MainActivity.this, new String[]{READ_EXTERNAL_STORAGE},111);} and receive the permissions

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (grantResults.length > 0) {
            if (requestCode == 111 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                textView.append("Permission granted write\n");

                // Create Uri
                File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                File file1 = new File (downloads + "//app-debug.apk");//downloads.listFiles()[0];
                Uri contentUri1 = getUriForFile(this, BuildConfig.APPLICATION_ID, file1);

                // Intent to open apk
                Intent intent = new Intent(Intent.ACTION_VIEW, contentUri1);
                intent.setDataAndType(contentUri1, "application/vnd.android.package-archive");
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(intent);
            }
        }
    }
}

file_paths.xml

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM