The solution to the update apk above android8.0 does not pop up the installation interface

     The code written before updating the apk was adapted to the version of android7.0. After the download of android8.0 and android9.0 was successful, the screen flickered for a while and did not jump to the system installation interface. Then check the information and know that android8.0 has added The new security measures do not allow the installation of applications that have not been verified by Google play , so we have to adapt to the 8.0 and 9.0 systems.

The first step is to write permissions in the xml file

  • Add the following permissions to the manifest file:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/> 

It was found to be of no use. It turned out that there was no permission to install unknown applications.

Determine whether you have obtained permission to install unknown applications 

In the PackageManager class in 8.0, the method canRequestPackageInstalls() is added to check whether the installation of unknown applications has been obtained.

if (Build.VERSION.SDK_INT>=26){
    boolean hasInstallPermission = isHasInstallPermissionWithO(mContext);
    if (!hasInstallPermission) {
        startInstallPermissionSettingActivity(mContext);
        return;
    }

}

@RequiresApi(api = Build.VERSION_CODES.O)
private boolean isHasInstallPermissionWithO(Context context){
    if (context == null){
        return false;
    }
    return context.getPackageManager().canRequestPackageInstalls();
}

The startInstallPermissionSettingActivity method is: Open the interface for setting and installing applications from unknown sources. It is best to write the package name of the apk and jump directly to the current apk permission interface.

 

/** 
 * Open the interface for setting and installing applications from unknown sources 
 * @param context 
 */ 
@RequiresApi(api = Build.VERSION_CODES.O) 
private void startInstallPermissionSettingActivity(Context context) { 
    if (context == null) { 
        return; 
    } 
    Intent intent = new Intent(); 
    //Get the current apk package URI and set it in the intent (this step allows the "unknown application permission setting interface" to only display the setting items of the current application) 
    Uri packageURI = Uri.parse("package :" + context.getPackageName()); 
    intent.setData(packageURI); 
    //Set the action of different versions to jump to unknown applications 
    if (Build.VERSION.SDK_INT >= 26) { 
        //intent = new Intent(android.provider .Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,packageURI);
        intent.setAction(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES); 
    } else {
        intent.setAction(android.provider.Settings.ACTION_SECURITY_SETTINGS); 
    } 
    ((Activity) context).startActivity(intent); 
    Toast.makeText(mContext, "Please enable unknown application installation permissions", Toast.LENGTH_SHORT).show(); 
}

In this way, mobile phones above 8.0 will be successfully adapted~

 

Guess you like

Origin blog.csdn.net/qq_22576071/article/details/97010225