The previous project targetSdk was developed based on Android6.0, that is, the API version number is 23. Now it needs to be upgraded to 9.0, and adaptation needs to be carried out across several versions. There is no way to do that.

7.0 adapter

File Sharing Adaptation

1. You need to be there firstAndroidManifest.xmlFile, addprovidernode

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>
Copy the code

2. And then you have toresDirectory created under the folderxmlFolder, and createfile_paths.xmlfile

<? xml version="1.0" encoding="utf-8"? > <! -- 7.0 Paths XMLNS: Android ="http://schemas.android.com/apk/res/android">
    <external-path name="my_download" path="download"/ > <! <external-path name= --> <external-path name= --> <external-path name="my_feedback" path="feedback"/>

</paths>
Copy the code

The following child nodes are supported within the Paths node:

  • <root-path/>Represents the root directory of the devicenew File("/")
  • <files-path/>On behalf ofcontext.getFilesDir()
  • <cache-path/>On behalf ofcontext.getCacheDir()
  • <external-path/>On behalf ofEnvironment.getExternalStorageDirectory()
  • <external-files-path>On behalf ofcontext.getExternalFilesDirs()
  • <external-cache-path>On behalf ofgetExternalCacheDirs()

Each node supports two attributes:

  • name
  • Path A path is a subdirectory of a directory, for example:
<external-path
        name="external"
        path="download" />
Copy the code

On behalf of the directory is: Environment. External.getexternalstoragedirectory ()/download, the other in the same way.


8.0 adapter

Android8.0 Notification bar adaptation

The notification bar on Android8.0, if your targetSdk is 26 or above and has not been adapted, will not show even if your app has notifications enabled

To do this, add the following code to your project Application and call it in the onCreate() method

private void initMessageChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = "channelId";
        String channelName = "channelName";
        String description = "description"; / / notification level int importance = NotificationManager. IMPORTANCE_HIGH; NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); channel.setDescription(description); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); assert notificationManager ! = null; notificationManager.createNotificationChannel(channel); }}Copy the code

There are four types of notification levels. You can set multiple notification levels according to your specific requirements. The default notification level will exist even if you do not set it.

  • IMPORTANCE_MINIf the notification function is enabled, it does not pop up, but there is no prompt tone and no display in the status bar
  • IMPORTANCE_LOWIf the notification function is enabled, it will not be displayed in the status bar
  • IMPORTANCE_DEFAULTIf the notification function is enabled, it will not pop up. A prompt tone will be displayed in the status bar
  • IMPORTANCE_HIGHIf the notification function is enabled, a prompt tone will be displayed in the status bar

8.0 APK Cannot be automatically installed

After Android8.0, unknown app installation permission is disabled by default, and the permission entry is hidden.

1. You must add permissions to the manifest file

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

2. Check whether the OS 8.0 or higher has the permission to install applications

if(build.version.sdk_int >= build.version_codes.o) { TargetSdkVersion is more than 26 to acquire correct canRequestPackageInstalls, otherwise has been returnedfalse
    boolean hasInstallPermission = context.getPackageManager().canRequestPackageInstalls();
    if(! hasInstallPermission) { Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ((Activity)context).startActivityForResult(intent,REQUEST_CODE_APP_INSTALL);return; }}else{install(context,filePath)} // Install APK method public static void install(context,filePath) String filePath) { try { File file = new File(filePath); Intent intent = new Intent(Intent.ACTION_VIEW);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri apkUri = FileProvider.getUriForFile(context, "com.jinr.core.provider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            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(); }}Copy the code

8.0 System Frame (Suspension frame) does not take effect Adaptation

Applications in 8.0 can only use TYPE_APPLICATION_OVERLAY window types to create hover Windows. Other window types were deprecated in 8.0. If any of the following window types exist in your application to create a hover box, they will need to be adapted as 8.0 no longer supports them.

  • TYPE_PHONE
  • TYPE_PRIORITY_PHONE
  • TYPE_SYSTEM_ALERT
  • TYPE_SYSTEM_OVERLAY
  • TYPE_SYSTEM_ERROR
  • TYPE_TOAST

When using these types of hoverboards, you need to add version judgment

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
	window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else {
	window.setType(WindowManager.LayoutParams.TYPE_TOAST);
}
Copy the code

8.0 Static broadcast cannot be used

In Android8.0, Google has made it clear that apps cannot use most of the implicit broadcasts registered in their manifest files, known as static broadcasts. So it’s best to dynamically register all the broadcasts you use.


9.0 adapter

9.0 Disable plaintext transmission adaptation

Android9.0 forbid network plaintext transmission, need to adapt

1. InresDirectory created under the folderxmlFolder, and createnetwork_security_config.xmlfile

<! - the Android 9.0 system does not allow clear network transmission need to add the configuration of the @ author Steven - > < network ws-security - config > < base - config cleartextTrafficPermitted ="true" />
</network-security-config>
Copy the code

***2. In the androidmanifest.xml file, add the following code under the application node

<? xml version="1.0" encoding="utf-8"? > <manifest ... > <application ... android:networkSecurityConfig="@xml/network_security_config". >... </application> </manifest>Copy the code

This method can support both 7.0 packet capture and 9.0 plaintext request

, of course, if you don’t need caught function, as well as a simple way to configure, directly in the listing file to join the android: usesCleartextTraffic = “true” attribute

<? xml version="1.0" encoding="utf-8"? > <manifest ... > <application ... android:usesCleartextTraffic="true". >... </application> </manifest>Copy the code

9.0 Apache Http request error adaptation

Android 9.0 error occurs when using Volley in a project, indicating a ProtocolVersion exception.

Add the following code under the Application node in the androidmanifest.xml file

<? xml version="1.0" encoding="utf-8"? > <manifest ... > <application ... > <uses-library android:name="org.apache.http.legacy" android:required="false"/>
        ...
    </application>
</manifest>

Copy the code

9.0 Use front end service adaptation

To use foreground services on Android 9.0, you need to add permissions

<? xml version="1.0" encoding="utf-8"? > <manifest ... > <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
</manifest>

Copy the code

Of course, here is just a list of some common adaptation, specific encountered other problems, also have to be adapted. Meet again later and then complement it.