5

I am Following these steps but for sdk version N ,android system showing a alert dialog box "Package installer has stopped" while installing the app.

: 1 - Add the following to the AndroidManifest.xml:

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

2 - Add the following paths.xml file to the xml folder(if not exist create it) on res in src, main

 <?xml version="1.0" encoding="utf-8"?>
 <paths xmlns:android="http://schemas.android.com/apk/res/android">
 <external-path
 name="external_file"
 path="."/>
</paths>

The pathName is that shown in the exemplary content uri example above and pathValue is the actual path on the system. It would be a good idea to put a "." for pathValue in the above if you do not want to add any extra subdirectory.

3 - Write the following code to Run Your Apk files:

File file = "path of yor apk file";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 Uri fileUri = FileProvider.getUriForFile(getBaseContext(), 
 getApplicationContext().getPackageName() + ".provider", file);
 Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
 intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true) ;
 intent.setDataAndType(fileUri, "application/vnd.android" + ".package-
 archive");
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | 
 Intent.FLAG_ACTIVITY_NEW_TASK);
 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 startActivity(intent);

} else {
   Intent intent = new Intent(Intent.ACTION_VIEW);

  intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-
  archive");
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);
}
raman
  • 65
  • 4
  • As i know u cannot install a application programatically from another application , at the max u can download the file , i also tried same before some days, it requires users permission to install the app. – sunilkarkala Oct 18 '17 at 05:03
  • 1
    it is working perfectly for every version except android N – raman Oct 18 '17 at 05:34

1 Answers1

1

First, set the target SDK Version to 26 (Android Oreo) to make everything to work.

Then follow these steps below:

  1. How to check if the installation is allowed?

You can check everywhere using getPackageManager().canRequestPackageInstalls() in your activity. Note that this boolean rerurns always false if you dont declare that permission or selecting the wrong SDK Version.

  1. What permission i need to request?

You need to declare Mainfest.permission.REQUEST_PACKAGE_INSTALLS into your app manifest, so here is it:

<uses-permission android:name="android.permission.REQUEST_PACKAGE_INSTALLS" />
  1. How can i prompt user to grant permission?

Here you can do something like this:

startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:".concat("your.package.name"))));
  1. How to prompt user to install apk?

Once you have completed all other steps, you can prompt the user to install package using this code:

Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
installIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true); //this is necessary if you want to know if the installation was success, failed or cancelled.
installIntent.setData(Uri.fromFile(new File("/sdcard/yourapk.apk"))); //replace yourapk to your apk name
startActivityForResult(installIntent, 1);

You may need also to add installIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true); if you want to know if the installation was success, failed or cancelled.