Pain points in Android model adaptation [sharing dry goods]

Source: Internet
Author: User

Pain points in Android model adaptation [sharing dry goods]

The birth of the Android platform has made great contributions to the popularity of smart mobile phones, but its biggest drawback is becoming increasingly prominent, that is, serious fragmentation: a wide range of devices, a large number of brands, and different versions, chips, cameras, and inconsistent resolutions all gradually become obstacles to the development of the Android system. Fragmentation not only leads to chaos in the Android system, but also increases the invisible development cost of Android applications. This article describes in detail the wide range of Android adaptation issues.

1. Personalized Launcher

Although the shortcut seems to be a small feature, it involves many model adaptation problems.

Code for creating shortcuts:

Intent addShortCut = new Intent (com. android. launcher. action. INSTALL_SHORTCUT); addShortCut. putExtra (Intent. EXTRA_SHORTCUT_NAME, title); // duplicate addShortCut cannot be created. putExtra (duplicate, false); addShortCut. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, icon); addShortCut. putExtra (Intent. EXTRA_SHORTCUT_INTENT, intent); sendBroadcast (addShortCut );

1. Unable to create shortcuts

More and more mobile phone manufacturers have removed the concept of shortcuts. As a result, we cannot use code to create shortcuts that we actually need. Data is displayed, and such mobile phones account for about 13%.

2. Duplicate shortcuts

In general, we do not want our shortcuts to be created again. Use addShortCut. putExtra (duplicate, false); The method can achieve the goal, but there are at least 8% of mobile phones on the market, even if duplicate is set to false, you can still create shortcuts again.

Representative mobile phone brands include Huawei, ZTE, and HTC.

Android Launcher source code:

2.1 duplicate shortcut creation solution V1.X

The earliest Method to Solve repeated creation of shortcuts is to delete the shortcuts before they are created. This method is actually very clever, because even if the shortcut does not exist, there will be no exception in performing the delete operation. In this way, it seems that the problem is solved too easily, but it is a pity that there is an adaptation problem in the delete shortcuts, and the data shows that about 21% of mobile phones cannot delete shortcuts normally.

Another method is to manually Save the creation record of the shortcut and use a field to record whether the shortcut has been created, so as to determine whether to create a new shortcut. This is also because a small upgrade is made to the solution after the shortcut cannot be deleted. Although it can solve the problem, if the program is cleared, everything is messy, we still cannot completely avoid repeated problems.

2.2 duplicate creation of shortcuts solution V2.X

Let's look at the source code for an unsolvable problem. The Android Launcher source code will not only judge the duplicate value when creating a shortcut, we will also query the database to see if the shortcut to be created already exists, and we will do it as well.

In addition, we have noticed that accessing the url uri when querying the database is a very important factor. The problem is that there are many database Uris and there are three Android-standard Uris:

 

In versions earlier than 2.2, the URI is: content: // com. android. launcher. settings/favorites? Required y = true

2.2 ~ Version 4.3 URI: content: // com. android. launcher2.settings/favorites? Required y = true

All versions above 4.4 are currently: content: // com. android. launcher3.settings/favorites? Required y = true

 

Not only does Android own a large number of Launcher database addresses, but the vendor's own defined addresses are more colorful. For example, the OPPO R827T access URI is: content: // com. oppo. launcher. settings/favorites? Running y = true; the HTC Z715e access address is: content: // com. htc. launcher. settings/favorites? Required y = true. In fact, there are far more than this, there are countless third-party Launcher applications, and many developers will also modify the database access address. Currently, there are up to 40 different access addresses.

 

  • Query the URI by permission:

     

    I believe everyone is familiar with querying the corresponding URI through the database's read and write permissions. It seems like I have found the ultimate solution, and let's take a look at it...

     

    1. Question 1: If you use the full permission for query-there are many permissions, we currently have more than 50.

    2. Question 2: If you use incomplete permissions to query (READ_SETTINGS), about 32% of mobile phones correspond to more than two Uris.

     

    For example:

    GT-I8262D:

    Authority: com. sec. android. app. launcher. settings ReadPermission: com. android. launcher. permission. READ_SETTINGS
    Authority: com. sec. android. app. launcher. settings. id ReadPermission: com. android. launcher. permission. READ_SETTINGS

    Lenovo A278t:

    Authority: com. aspire. mm. Settings ReadPermission: com. aspire. mm. permission. READ_SETTINGS
    Authority: com. huaqin. launcherEx. settings ReadPermission: com. huaqin. launcherEx. permission. READ_SETTINGS
    Authority: com. huaqin. themgr. Settings ReadPermission: com. huaqin. thememgr. permission. READ_SETTINGS

    2. Colorful Camera1. Intent calls the camera program in the mobile phone

    If we have set the photo storage path, we may encounter three problems:

     

    • Problem 1: The data returned by the onActivityResult method is Null (data indicates that the data of the 93% model will be Null. Therefore, if we specify a path, do not use data to obtain the photo, at least make a short judgment before use ).

      Problem 2: Photos cannot be stored.

       

      If the custom storage path is/mnt/sdcard/lowry/, and the mobile phone SD card does not have a folder named lowry before taking the photo, some pictures will not be saved after taking the photo, as a result, we cannot get photos. Most mobile cameras will create non-existent folders if the folder does not exist, but some mobile phones will not. The model is: samsung I8258, Huawei H30-T00, Red Rice and so on.

      The solution is to determine whether all folders in the path exist before specifying the storage path. If no folder exists, create the folder first and then call the camera.

       

      • Problem 3: photos can be stored, but their names are incorrect.

         

        File: // mnt/sdcard/123 1.jpg, because the fromFile method of URI replaces spaces in the path with "% 20.

        In fact, this is not a problem for most mobile phones. when parsing the storage path, the mobile phone will replace "% 20" with a space. In this way, the final photo name is the name we specified at the beginning: 123 bytes. We can find the photo in the path "file: // mnt/sdcard/123 1.jpg!

        Summary:

        (1) Use intent (data) in onActivityResult to make a short judgment.
        (2) When specifying the photo path, check whether all the folders in the path exist. If no folder exists, create a folder and then call the camera to take the photo.
        (3) When you specify the photo storage path, do not include spaces or other special symbols in the photo name.

        2. Call the mobile phone Camera through the open method of Camera

        2.1 continuous auto focus crash

        Cause: the first focus is not over, and the application layer initiates the second focus, causing the focus to fail.

        Solution 1: Pass in AutoFocusCallback;

        Solution 2: delayed operations;

        Solution 3: exception capture.

        2.2 incorrect judgment on the number of cameras

        Symptom: When we use Camera. the getNumberOfCameras () method returns inaccurate results when detecting the number of cameras. If we try to open a non-existent Camera, an exception is thrown. This also reminds us to add exception protection when enabling the Camera.

        Representative Models: Lenovo 278 T, codu 8022

        2.3 flashlight judgment

        We often use the following two methods to determine whether a mobile phone has a flashlight:

        Method 1: Use the getSupportedFlashModes method;

        Method 2: Use PackageManager to determine whether the flashlight is supported.

        Method 1: 3.7% of the machine results are incorrect, and it is impossible to accurately determine whether the mobile phone has a flashlight. The main brands include codu, Tianyu, Lenovo, and Samsung. Method 2: 9.7% of the machine results are incorrect. The main brands include VIVO, gionee, cool, Tianyan, duowei, and Samsung.

        We recommend that you use these two methods when determining whether your phone has a flashlight. The probability of an error will be greatly reduced.

        2.4 switching between the always-on and other statuses

        The precondition is that the flashlight is set to always on (Parameters. FLASH_MODE_TORCH), and the flashlight is always on. In this case, after setting the flashlight mode to Parameters. FLASH_MODE_AUTO, the flashlight remains on normally. This model accounts for about 12% of the popular models. In this case, we need to first set the flashlight mode to Parameters. FLASH_MODE_OFF and then set other modes.

        2.5 After the release of Camera, the flash still shines

        Now that this is enabled, we have to take care of it. To be honest, this problem was not my consideration in the past, because Camera will be released when the Activity is destroyed or paused when we use Camera. At this time, no matter what the flash is, it will be turned off with the release of Camera. It was not until I met OPPO R815T that my world view had changed. If the flash was set to always bright, even if the Camera flash was released, it was still steady.

        And because Camera has been released, you can no longer turn off the flashlight. Close the App and uninstall the App. You can still turn off the battery ..... therefore, if your program has an operation to set the flashlight to always on, we recommend that you set the flashlight to off before releasing Camera (Parameters. FLASH_MODE_OFF) status.

        2.6 alternative situations of CameraInfo

        The official documentation provides examples of how to adjust the camera preview angle:

        In this example, CameraInfo is very important. The final angle calculation is based on the orientation Value in CameraInfo. Therefore, if this value is inaccurate, our angle may be incorrect.

        The orientation Value of CameraInfo obtained by VIVO V1 is 90 for the first time, while mCamera = Camera is executed. open (); then you can get the orientation Value of CameraInfo to 0, and get 0 in the future, unless you restart your mobile phone.

        No matter which app on the mobile phone, after one Camera. open () is executed, the orientation of obtaining CameraInfo in all other programs is 0.

        The camera that comes with the mobile phone can use the decompilation System camera, and it turns out that the system camera has not corrected the angle as shown in the official example.

         

         

        Solution:

         

        1. Follow the camera of the mobile phone system;

        2. The orientation value of the mobile phone CameraInfo is set to 90.

        3. Dual-card

        The basic solution to dual-card problems:

         

        1. inference: the built-in system apps of mobile phones can use these functions normally, so there must be vendor-defined APIs to implement these functions;

        2. decompilation: Framework, system App, and system database;

        3. Positioning: TelephoneManager extension, SMSManager extension, telephone service extension, SMS service extension, and database field extension.

         

        Iv. UI adaptation

        Speaking of UI adaptation, it is a headache. The following figure shows the work of a product for UI adaptation. It can be seen that it is quite tedious.

        In addition to resolution adaptation, sometimes a label in the layout file may cause some problems. Let's take a look at the following layout code:

        Correct results:

        Error result:

        This is because the layout_marginTo label is used in FrameLayout versions earlier than Android 3.0. gravity must be set to take effect.

        How can this problem be solved? In the component that sets android: layout_marginTop, set android: layout_gravity = top.

        5. More amazing 1. Vendor's abstract methods

        If you need to implement the InputConnection interface, pay attention to the following exception:

        Decomcompiled the Framework of this mobile phone and found that the vendor added an abstract method named performYLPrivateCommand in the InputConnection interface.

        2. distance sensor

        2.1 different phone event. values [0] values are ever changing

        To put it simply, there are several representative examples:

         

        1. Some mobile phones are normal, and when they are close to 0, they are 1 (0, 1 );

        2. Mobile phone numbers with a small personality will increase, such as (0,100), (3,100), (), and so on;

        3. The number of 213 mobile phones is rather inexplicable. (1.001, 5.003), do you mean high accuracy?

         

        2.2 The relationship between values and distance is inconsistent

        Since we use a number to determine whether the current situation is in the near-ear state, is it necessary to say the size of this value? The number of mobile phones that I have seen is smaller than that of mobile phones that I have seen. However, there are several mental cell phones (100 million) that are close to each other and are larger than those close to each other. This is a pitfall, so developers should pay attention to it ~~!!

        2.3 The Return Value of the getMaximumRange method is incorrect.

        There is an API: SensorManager. getdefasensensor (Sensor. TYPE_PROXIMITY ). getMaximumRange (), the document explains that this should obtain the maximum range of changes in the sensor value. For example, if the value is 0 when it is near, the value is 1 when it is far away. Therefore, the value of getMaximumRange () should be 1, which will not affect our judgment. Here I am only referring to the API and our daily usage habits. If this is not the case, it will cause trouble for our programming.

         

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.