Android 10 adapter

Time:2022-1-3

1. Android 10 cannot obtain IMEI information. Use the following code to obtain it

 public  String getIMEIDeviceId(Context context) {

        String deviceId;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
        {
            deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } else {
            final TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    return "";
                }
            }
            assert mTelephony != null;
            if (mTelephony.getDeviceId() != null)
            {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                {
                    deviceId = mTelephony.getImei();
                }else {
                    deviceId = mTelephony.getDeviceId();
                }
            } else {
                deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            }
        }
        Log.d("deviceId", deviceId);
        return deviceId;
    }

2. Android 10 can’t get the system file. The solution is in manifest The following configuration is added to the application tag in XML. This attribute can only be used in api29 and above

 <application
        android:requestLegacyExternalStorage="true">

3. After Android 10, the sideslip menu uses Android X drawerlayout. widget. DrawerLayout
4. HTTP request failed
(1) Add network security configuration. First, create a new XML folder in the res directory and add network_ security_ config. XML file:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

(2)AndroidManifest. Add application in XML:

<manifest ... >
    <application android:networkSecurityConfig="@xml/network_security_config">
            ...
    </application>
</manifest>

(3) This is a simple and crude configuration method, which either supports HTTP or does not support HTTP. For security and flexibility, we can specify the supported HTTP domain names:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <!--  HTTP -- > is used for some domain names on Android 9.0
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">aaa.example.com</domain>
    </domain-config>
</network-security-config>

5. Apache HTTP client is deprecated. Starting from Android 9.0, the library has been removed from bootclasspath by default. If you want to use it, you need to use it in the application’s androidmanifest Add to XML file:

<manifest ... >
    <application>
        <uses-library android:name="org.apache.http.legacy" android:required="false"/>
            ...
    </application>
</manifest>