android Installation, Unloading, Opening Apk Adaptation 7.0

Keywords: Android FileProvider xml

In addition to android installation apk needs to be adapted. Open and uninstall do not need to be adapted, there is not much to say, the code is very simple, are to start an Activity,,,,,,

I. Installation of apk

With the upgrade of android version, android's privacy protection is becoming more and more powerful.

For example, Android 6.0 introduced Runtime Permissons. Android 7.0 also introduced the StrctMode Api policy

Because Android 7.0 private directories are restricted access, FileUriExposedException exceptions are reported when an apk is installed in Android 7.0

Using FileProvider to solve the problem of restricted access to Android 7.0 private directories

Steps for using FileProvider

1. Register provider in AndroidManifest.xml manifest file, because provider is also one of the four components, similar to ContentProvider component, which provides data to the outside world. All under the Application node

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.app.rui.appstore.fileProvider"
        android:exported="false"
        android:grantUriPermissions="true">

        <!-- metadata -->
        <meta-data
               android:name="android.support.FILE_PROVIDER_PATHS"
               android:resource="@xml/file_paths"/>
</provider>

Attention:
Export: Must be false, true will report security exceptions
grantUriPermissions: Provisional access to URI is granted for true.
Authority: Component identity, like ContentProvider, for unique identity, general package name +". fileProvider"
resourece: Specify the XML configuration file under res "@xml/file_paths" Note that the file name can be customized in the XML directory

2. Create the directory and xml specified by resource

<paths>

    <external-path path="Download" name="download"/>

</paths>
The root directory represented by <files-path/>: Context.getFiesDir()
The root directory represented by <external-path/>: Environment.getExternal Storage Directory ()
The root directory represented by <cache-path/>: Context.getCacheDir()

Note: path = "" specifies that you can access the root directory and its subdirectories

For example: path = Download, which means that you can access / storage/emulated/0/Download directory, and subdirectories

3. Use FileProvider to get Uri

    public static boolean installApk(Context context, String filePath) {
        File file = new File(filePath);
        if (!file.exists() || !file.isFile() || file.length() <= 0) {
            return false;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        // Since Activity is not started in the Activity environment, set the following label
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri apkUri = null;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            apkUri = FileProvider.getUriForFile(context, "com.app.rui.appstore.fileProvider", file);
            //Add this sentence to indicate that the file represented by the Uri is temporarily authorized for the target application
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            apkUri = Uri.fromFile(file);
        }
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        context.startActivity(intent);
        return true;
    }
Note: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); this sentence must be under new Intent, otherwise it will make mistakes.

FileProvider.getUriForFile(Context  context, String authority, File file)
The authority parameter is actually the value of android:authorities under the <provider/> node in androidManfist.xml

This adapts the android installation apk code below 7.0 and 7.0

2. Open the installed app

    public static void runApp(Context context, String packagename) {
        context.startActivity(new Intent(context.getPackageManager().getLaunchIntentForPackage(packagename)));
    }

3. Uninstall third-party apps. If it is a system app, you need to get Root privileges, but don't introduce it for the time being.

    public static boolean uninstallApk(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            return false;
        }
        Intent i = new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + packageName));
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
        return true;
    }

Posted by nileshkulkarni on Sat, 22 Jun 2019 17:59:49 -0700