3

I am trying to install an APK on android N

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        File apkFile = new File(Environment.getExternalStorageDirectory() + "/my/myapk.apk");
        Uri apkURI = FileProvider.getUriForFile(mContext,
                BuildConfig.APPLICATION_ID + ".provider",
                apkFile);
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(apkURI);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        mContext.startActivity(intent);
    }

The app is installing but it is not opening the app.

On an SDK below N with the below code it was installing and after installation it would open the app.

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

On android N how can I install APK through code and then open once installation is complete?

Ryan
  • 1,863
  • 13
  • 20
andro
  • 1,007
  • 1
  • 14
  • 32
  • Unfortunatelly package installer "doesn't like" content uris ... you have to use your old way ... I think CommonsWare asked question about it ... – Selvin Jan 20 '17 at 11:06
  • but its not working on android n (old way) – andro Jan 20 '17 at 11:06
  • found it: http://stackoverflow.com/questions/9637629/can-we-install-an-apk-from-a-contentprovider – Selvin Jan 20 '17 at 11:07
  • @Selvin: `content` is supported on Android 7.0+. With `FileUriExposedException`, it was required. Otherwise, apps with `targetSdkVersion` of 24 or higher could not install apps. – CommonsWare Jan 20 '17 at 12:21
  • hmm I use `intent.setDataAndType(fileUri, "application/vnd.android.package-archive");` and it's working (with content uri on android 7.1 emulator) – Selvin Jan 20 '17 at 12:21
  • " app is installing but its not opening the app" -- please explain **in detail** what "its not opening the app" means. – CommonsWare Jan 20 '17 at 12:21
  • @Selvin: Your `targetSdkVersion` is below 24. – CommonsWare Jan 20 '17 at 12:24
  • *with **content uri** on android 7.1* and no, it's 25 ... my "hmm" comment means same as yours *app is installing but its not opening the app* => it's working for me (the first was wrong as i didn't know that they support it now) ... at least after that question I fix an error in my app (which doesn't occurred yet as users don't use > android 6) – Selvin Jan 20 '17 at 12:25
  • OK now I see the problem ... the problem is prolly with flags ... the code is prolly used from Service and he needs FLAG_ACTIVITY_NEW_TASK ... `setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);` should fix it – Selvin Jan 20 '17 at 12:31
  • @CommonsWare so when i install app on below n it open apk and ask for install . once installation done it ask user to open or cancel (screen comes) . but in N it just installing apk but after installation done its not asking to open the app . it just minimising – andro Jan 23 '17 at 02:45
  • its not helping – andro Jan 24 '17 at 12:22
  • @andro: Did you find the solution? – Alireza Noorali Dec 18 '17 at 05:51
  • For me, the older approach is working even in Nougat and Oreo...... – GvSharma Mar 30 '18 at 11:21

0 Answers0