Android 7.0-8.0 app update

When you want to go online, you have to update the interface when you make an app.

First of all, you need to pack when doing validation, uninstall the AS installation, and then install the packaged apk.

7.0 and 8.0 require permissions

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

Add in Android Manifest:

/*

Note: When you test on some mobile phones, you will find that you have been doing it all the time.

Uri apkUri = FileProvider.getUriForFile(Frag_Set_Set.this, BuildConfig.APPLICATION_ID+".provider", file);

It's always stuck here; probably for the same reason I started out with, in BuildConfig.APPLICATION_ID+".provider" and authorities= "your package name. provider", authorities= "your package name. fileprovider" are used, so just change the fileprovider to provider in both places.

*/
 

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="Your package name.provider"
            android:grantUriPermissions="true"
            android:exported="false"
            >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

How to download the installation package? There are a lot of textbooks on the Internet. After downloading, I installed and updated this card for half a day!

7.0-8.0 installation is not much worse, but 8.0, need to apply for permission

 /**
     * Determine whether 8.0, 8.0 need to deal with unknown application source permissions, otherwise install directly?
     */
    private void checkIsAndroidO(File file) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            boolean b = getPackageManager().canRequestPackageInstalls();
            if (b) {
                installApk(file);//Install application logic (write your own)
            } else {
                //Request permission to install an unknown application source
                new ToastUtil().showToast(Frag_Set_Set.this,"Open permission to install applications from unknown sources,Otherwise, it can't be installed properly!",2000);
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, 0);
            }
        }else {
            installApk(file);
        }

    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 0:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    installApk(file);
                } else {

                    //No permission to set your app package name in yourAppPackageName
                    Uri packageURI = Uri.parse("package:" + getPackageName());
                    Logutil.w(tag,"-->"+packageURI);
                    Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
                    Frag_Set_Set.this.startActivityForResult(intent, 1);
                }
                break;

        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                checkIsAndroidO(file);
                break;
            default:
                break;
        }
    }

Then it's installed:

/**
     * Install apk
     */
    private void installApk(File file) {
        if (Build.VERSION.SDK_INT>=24){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                boolean hasInstallPermission = getPackageManager().canRequestPackageInstalls();
                if (!hasInstallPermission) {
                    checkIsAndroidO(file);
                    return;
                }
//                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            i.addFlags(Intent
        .FLAG_ACTIVITY_NEW_TASK);//Open a thread to prevent flipping when it is opened after installation
            Uri apkUri = FileProvider.getUriForFile(Frag_Set_Set.this, BuildConfig.APPLICATION_ID+".provider", file);
            Logutil.w(tag,"-->"+BuildConfig.APPLICATION_ID+".provider");
            i.setDataAndType(apkUri, "application/vnd.android.package-archive");
            startActivity(i);
        }
        //Compatibility below 7.0
        else {
            try {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setDataAndType(Uri.parse("file://" + file.toString()), "application/vnd.android.package-archive");
                startActivity(i);
            }catch (Exception e){
            }

        }
    }

 /*

Note: When you install the rear hall, you flip back because you need to open a thread: i.addFlags(Intent. FLAG_ACTIVITY_NEW_TASK);

Android 7.0, 8.0 update app

*/

 

Tags: Mobile Android FileProvider xml

Posted on Thu, 24 Jan 2019 05:54:13 -0500 by ChessclubFriend