If you want to create a worst-case scenario on Google Play, the simple and crude way is to develop an app with an extremely slow interface, battery consumption, and memory consumption. Then you will get countless bad reviews from users, and no one will download it, and no one will use it. Even if your app design is creative, it will be useless. The editor shares the following ten suggestions for efficient Android development: Suggestion 1: How to cancel some actions in threads in the background We know that all operations during the running of the App are performed in the main thread (UI thread) by default, which will affect the response speed of the App, causing the program to freeze, crash, or even cause system errors. In order to speed up the response, you need to move time-consuming operations (such as network requests, database operations, or complex calculations) from the main thread to a separate thread. The most efficient way is to complete this operation at the class level. You can use AsyncTask or IntentService to create background operations. If you choose to use IntentService, it will start when needed and then process the request (Intent) through a worker thread. There are a few limitations to note when using IntentService: This class should not pass information to the UI. If you want to display the processing result information to the user, please use Activity; Only one request can be processed at a time; Each request processing process cannot be interrupted; Suggestion 2: How to keep the response from ANR Removing time-consuming operations from the UI thread can also prevent the user from seeing the system not responding (ANR) dialog box. All you need to do is inherit AsyncTask to create a background worker thread and implement the doInBackground() method. Another way is to create a Thread class or HandlerThread class yourself. Please note that this will also slow down the App, because the default thread priority is the same as the main thread priority unless you explicitly set the thread priority. Suggestion 3: How to initialize query operations in threads When the query operation is being processed in the background, the display of data is not instant, but you can use the CursorLoader object to speed up the process. This operation can keep the interaction between Activity and user unaffected. After using this object, your App will initialize an independent background thread for ContentProvider to query, and when the query is completed, it will return the result to the Activity that called the query. Recommendation 4: Other aspects that need attention Use StrictMode to check for potentially time-consuming operations in the UI thread; Use specialized tools such as Systrace or Traceview to find bottlenecks in your application; Use progress bars to show users the progress of operations; If the initialization operation is time-consuming, display a welcome screen. Optimize your device's battery life If your app consumes a lot of battery, please don't blame the user for uninstalling your app. For battery usage, the main battery consumption is as follows: Wake up the program frequently when updating data; Use EDGE or 3G to transmit data; Text data conversion, performing non-JIT regular expression operations. Suggestion 5: How to optimize the network If there is no network connection, let your app skip network operations; update data only when there is a network connection and no roaming; Select a compatible data format and convert all requests containing text data and binary data into binary data format requests; Use efficient conversion tools, consider using streaming conversion tools more often and use less tree-based conversion tools; For a faster user experience, please reduce repeated server access operations; If possible, use the framework's GZIP library to compress text data to use CPU resources more efficiently. Tip 6: How to optimize the application's work on the front end If you consider using wakelocks, try to set them to the lowest level possible; To prevent potential bugs from causing battery drain, specify a timeout explicitly; Enable the android:keepScreenOn attribute; In addition to the system GC operation, consider manually recycling Java objects, such as XmlPullParserFactory and BitmapFactory. There are also the regular expression Matcher.reset(newString) operation and StringBuilder.setLength(0) operation; Be aware of synchronization issues, although it is safe in the main thread; Use more reuse strategies in Listview; If possible, use rough network positioning instead of GPS. GPS requires 1mAh (25s * 140 mA), while the general network only uses 0.1mAh (2s * 180mA). Make sure to unregister GPS location updates, because these updates will continue in onPause(). When all apps unregister this operation, users can re-enable GPS in the system settings without wasting power; Consider using low-precision variables for heavy math operations and caching variable values when using DisplayMetrics for DPI tasks; Tip 7: How to optimize applications that work in the foreground Please make sure that the service life cycle is short-lived, because each process requires 2MB of memory and will be restarted when the foreground program needs memory; Keep memory usage small; If you want the app to update every 30 minutes, do it when the device is awake; It is not good for a Service to be in pull or sleep state, which is why you should use AlarmManager or configure the property stopSelf() when the service ends. Recommendation 8: Other considerations Before performing the overall update, check the battery status and network status, and wait for the best status before performing a major replacement operation; Allow users to see power usage, such as update cycles and background operations; Implementing low memory usage UI Tip 9: How to find layout display problems When we build UIs for layouts alone, we are creating apps that abuse memory and introduce obnoxious delays in the UI. The first step to achieving a smooth, low-memory UI is to search your app for potential bottleneck layouts. Hierarchy Viewer in SDK/tools/ Tooltools. Another good tool is Lint, which scans the source code of the application to find possible bugs and optimizes the control results. Tip 10: How to solve the problem If you find problems with the layout display results, you can consider simplifying the layout structure. You can convert the LinearLayout type to the RelativeLayout type to reduce the layout hierarchy. |
<<: Share: HTML5 game development experience and development tools
>>: Do you know what features App development can really improve customer retention rate?
We all know that common units for expressing dist...
I always feel that I am not a good analytical per...
Onmyoji is a phenomenal mobile game with over 10 ...
Produced by: Science Popularization China Author:...
The latest monitoring data from CCTV Market Resea...
This article mainly introduces how to cancel a Do...
In a study funded by the U.S. Department of Homel...
For many people, there is a desire to obtain cert...
After the user clicks on the ad, he or she will &...
There are genes from other species in the genome ...
Many people write brilliant copy and make beautif...
According to a recent comprehensive foreign news ...
Audit expert: Gu Haitong Deputy Chief Physician, ...
Although mini programs have been around for quite...
Content preview: [WeChat: jimifeng01] Red|sleeves...