10 suggestions for efficient Android App development,
If you want to make the most failed case on Google Play, the best secret is that the interface is extremely slow, consumes power, and consumes memory. Next, you will get negative comments from users, and your reputation will become bad. It is useless even if your applications are well designed and creative.
Every problem that affects product efficiency, such as power consumption or memory usage, affects App success. This is why it is crucial to ensure optimization and smooth operation during development without causing problems in the Android system. We don't need to discuss efficient programming here, because we don't care whether the code you write can withstand the test. Even efficient code execution takes time to run. This article describes how to shorten the running time as much as possible and how to develop your favorite apps.
How to efficiently use the thread I: How to cancel the actions in some threads in the background
We know that all the operations in the App running process are performed in the main thread (UI thread) by default, so that the App response speed will be affected. It will cause the program to get stuck, die, or even cause a system error.
To speed up the response, you need to move time-consuming operations (such as network requests, database operations, or complex computations) 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 up as needed, and then process the request (Intent) through a working thread ).
When using IntentService, pay attention to the following restrictions:
- Do not pass information to the UI. If you want to display the processing result information to the user, use Activity;
- Only one request can be processed at a time;
- Each request processing process cannot be interrupted;
Recommendation 2: how to keep the response from ANR
Remove time-consuming operations from the UI thread. This method also prevents user operations from appearing in the ANR dialog box. What needs to be done is inherit AsyncTask to create a background working thread and implement the doInBackground () method.
Another way is to create a Thread class or HandlerThread class. Note that this will also slow down the App, because the default thread priority is the same as that of the main thread, unless you explicitly set the thread priority.
Recommendation 3: How to initialize query operations in the thread
When the query operation is being processed in the background, the displayed data is not real-time, but you can use the CursorLoader object to speed up the process. This operation will not affect the interaction between the Activity and the user.
After using this object, your App will Initialize an independent background thread for ContentProvider to query. After the query is complete, it will return results to the Activity that calls the query.
Recommendation 4: Other considerations
- Use StrictMode to check possible time-consuming operations in the UI thread;
- Use some special tools such as Systrace or Traceview to find bottlenecks in your application;
- Displays the operation progress with a progress bar;
- If Initialization is time-consuming, a welcome page is displayed.
Optimized battery life of devices
If the application is too expensive, do not blame the user for uninstalling your application. For battery use, the main power consumption is as follows:
- The program is often awakened when data is updated;
- Use EDGE or 3G to transmit data;
- Converts text data to non-JIT regular expressions.
Recommendation 5: how to optimize the network
- If there is no network connection, please let your application skip network operations; update data only when there is a network connection and no roaming;
- Select a compatible data format to convert all requests that contain text data and binary data into binary data;
- The use of efficient conversion tools, streaming conversion tools, less tree conversion tools;
- For faster user experience, Please reduce repeated server access operations;
- If possible, use the GZIP library of the framework to compress text data to efficiently use CPU resources.
Recommendation 6: how to optimize the front-end work of the application
- If you want to use wakelocks, set it to the minimum level;
- To prevent power consumption caused by potential bugs, specify the timeout time;
- Enable the android: keepScreenOn attribute;
- In addition to the system's GC operations, you need to manually recycle Java objects, such as XmlPullParserFactory and BitmapFactory. There are also the Matcher. reset (newString) and StringBuilder. setLength (0) operations of the regular expression;
- Pay attention to the synchronization problem, although it is safe in the main thread;
- The Reuse Policy should be adopted in Listview;
- If it is allowed, use a rough network instead of GPS. Compared with GPS, 1 mAh (25 s * 140 mA) is required, while 0.1 mAh (2 s * 180mA) is used in general networks );
- Make sure that the location update operation of the GPS is canceled because the update operation will continue in onPause. When all applications cancel this operation, you can re-enable GPS in system settings without wasting power;
- Consider using low-precision variables in a large number of mathematical operations and caching variable values when using DisplayMetrics for DPI tasks;
Recommendation 7: how to optimize the front-end applications
- Make sure that the service life cycle is short, because each process requires 2 MB of memory, and the front-end program restarts when the memory is required;
- Keep the memory usage not too large;
- If you want to update the application every 30 minutes, perform this operation when the device is in the wake-up status;
- The Service status in pull or sleep is poor, Which is why AlarmManager or the attribute stopSelf () should be used at the end of the Service.
Recommendation 8: Other considerations
- Check the battery status and network status before the overall update, and wait for the best status to undergo a large change;
- Allows users to see the electricity usage, such as the update cycle and background operations;
UI 9: how to find the layout display problem
When we create a UI for the layout separately, we are creating an App with memory abuse. It will have a bad latency in the UI. To implement a smooth and Low-memory UI, the first step is to search your application to find out the potential bottleneck layout. Use the Hierarchy Viewer Tool in Android SDK/tools.
Another good tool is Lint, Which scans the application source code to find possible bugs and optimizes the control results.
Recommendation 10: how to solve the problem
If the layout display result shows a problem, you can consider simplifying the layout structure. The LinearLayout type can be converted to the RelativeLayout type to reduce the hierarchical structure of the layout.
More perfect and constantly optimized
Although each of the above suggestions seems to be a small improvement, if it can become a part of your daily code, you will see unexpected results. To enable Google Play to see more outstanding, smooth, fast, and power-saving applications, it is a step towards the perfect target of Android.