Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

install apk programmatically in android 8 (API 26)

Tags:

android

apk

I wrote a method to install apk in all version of android and it works until android 8 . but it seems android 8 do not response to this method

install_apk(File file) {         try {             if (file.exists()) {                 String[] fileNameArray = file.getName().split(Pattern.quote("."));                 if (fileNameArray[fileNameArray.length - 1].equals("apk")) {                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {                         Uri downloaded_apk = getFileUri(context, file);                         Intent intent = new Intent(Intent.ACTION_VIEW).setDataAndType(downloaded_apk,                                 "application/vnd.android.package-archive");                         intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);                         context.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);                         context.startActivity(intent);                     }                 }             }         } catch (Exception e) {             e.printStackTrace();         }     } 

and this method for get uri on api >= 23

Uri getFileUri(Context context, File file) {         return FileProvider.getUriForFile(context,                 context.getApplicationContext().getPackageName() + ".HelperClasses.GenericFileProvider"                 , file);     } 
like image 211
Vahid Avatar asked Aug 17 '17 04:08

Vahid


People also ask

How do I manually install APK files on Android?

Just open your browser, find the APK file you want to download, and tap it – you should then be able to see it downloading on the top bar of your device. Once it's downloaded, open Downloads, tap on the APK file and tap Yes when prompted. The app will begin installing on your device.

Can I install APK in Ubuntu?

If you run an APK in Ubuntu with Anbox, it will install like any other Android app. You have two options for installing apps on Anbox: Sideload. Install Google Play.


1 Answers

You should be add a new permission.

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/> 
like image 144
Gavin Liu Avatar answered Oct 08 '22 17:10

Gavin Liu