whether the hammer or the eggplant phone keeps popping up, Android's handset market share is still the biggest, so the number of apps based on Android is huge. So, how can you develop a higher-performance Android App? It is a big headache for software development companies and programmers in general. Today, we offer a few tips to improve The performance of Android apps.
Efficient use of threads
1. Cancel actions in some threads in the background
We know All operations in the app run are done by default in the main thread (UI thread), so the app 's responsiveness is affected. can cause the program to stutter, die, or even get a system error.
to speed up responsiveness, it is necessary 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 to do this is to do this at the class level , and you can use asynctask or intentservice to create a background operation. If you choose to use intentservice, it starts up when needed and then processes the request (Intent) through a worker thread.
Use There are a few limitations to note when Intentservice:
· This class does not give UI Pass information, if you want to show the user the processing result information, please use Activity ;
· only one request can be processed at a time;
· Each processing request process can not be interrupted;
2. maintain response does not occur in ANR
from removing time-consuming actions in the UI thread can also prevent user actions from appearing in 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 The Thread class or the handlerthread class. It is important to note that this also slows down the App because the default thread priority is the same as the primary, unless you explicitly set the priority of the thread.
3. initializing query operations in the thread
When the query operation is being processed in the background, the presentation data is not instantaneous, but you can use the Cursorloader objects to speed up, this operation can make The interaction between Activity and user unaffected.
after using this object, your The App Initializes a separate background thread for ContentProvider to query, and when the query is finished, it returns results to the Activity that invoked the query .
4. Other areas to be aware of
· Use Strictmode to check UI potentially time-consuming operations in a thread;
· use a few special tools such as Safe.ijiami , Systrace or Traceview to find bottlenecks in your application;
· Use the progress bar to show the user the progress of the operation;
· If the initialization operation is time consuming, please show a welcome interface.
optimize the battery life of your device
If the app is very power-consuming, don't blame the user for uninstalling your app. For battery use, the main charges are as follows:
· Wake up the program frequently when updating data;
· with EDGE or 3G to pass data;
· text data conversion, for non- JIT the regular expression operation.
5. Optimize your network
· If you don't have an Internet connection, let your app skip network operations and update the data only if there is a network connection and no roaming;
· Select a compatible data format to convert all requests containing textual data and binary data into binary data format requests;
· using efficient conversion tools, consider using streaming conversion tools, less tree-shaped conversion tools;
· for a faster user experience, reduce the duplication of access to the server;
· If you can, please use Framework of the GZIP Library to compress text data for efficient use CPU Resources.
6. optimize the work of the application in the front end
· If you consider using Wakelocks , try to set it to the minimum level;
· to prevent the potential Bug the resulting power consumption, please specify the time-out period explicitly;
· enabled Android:keepscreenon attributes;
· In addition to the system GC operation, more consideration of manual recycling Java objects, such as xmlpullparserfactory and the bitmapfactory . There are also regular expression matcher.reset (newstring) operations,stringbuilder.setlength (0) operations;
· be aware of the problem of synchronization, although it is safe in the main thread;
· in the Listview The reuse strategy should be adopted more.
· if allowed, use rough network positioning instead of GPS , compare GPS need to 1mAh ( 25s * MA ), while the general network uses only 0.1mAh ( 2s * 180mA );
· ensure logoff GPS Update operation, as this update operation is OnPause () will continue in the same. When all applications are logged out of this operation, the user can re-enable GPS in the system settings without wasting power;
· consider using low-precision variables in a large number of mathematical operations, and Displaymetrics make DPI cache variable values during task;
7. the application of the optimization work in the foreground
· Please make sure Service The life cycle is short, because each process requires 2MB memory, and it restarts when the foreground program needs memory;
· keep the amount of memory in use not too large;
· if you want to apply each - The minute is updated, please do it while the device is awake;
· Service is not good in pull or sleep State, which is why it is used at the end of the Alarmmanager or configures the reason for the property stopself () .
8. Other precautions
· Check the status and network status of the battery before making the overall update, and wait for the best state for a large-scale reload operation;
· let the user see electricity situation, such as update cycle, background operation;
Implementing a low memory footprint UI
9. Find the layout display problem
when we create a UI for the layout alone, we create an Appthat abuses memory , and it has a nasty delay in the UI . To achieve a smooth, low-footprint UI, the first step is to search your app to find out the potential bottleneck layout. Use the Hierarchy Viewer Tool that comes with Safe.ijiami and Android sdk/tools/ Tools.
and there's a good tool for that. Lint, it scans the application's source code for possible bugsand optimizes control results.
Solving the problem
If the layout shows a problem, you can consider simplifying the layout structure. You can convert the linearlayout type to a relativelayout type, reducing the hierarchy of the layout.
The pursuit of perfection and continuous optimization
with each of these tips, hopefully it will be part of your everyday code, and you'll see unexpected results. For Google Play to see more outstanding, smooth, faster, and more power-saving applications, take a step toward the perfect goal of Android.
Source: Code Agricultural Network
Top ten tricks to optimize Android app performance