10 tips for efficient App programming

  • 2020-09-16 07:46:52
  • OfStack

This article presents 10 recommendations for efficient App programming. To share for your reference, the details are as follows:

If you want to do one of the biggest failures on Google Play, the best secret is that the interface is extremely slow, power-hungry, and memory-hungry. Then you get negative comments from users, and you end up with a bad reputation. It doesn't matter if your app is well designed and creative.

Each problem that affects product efficiency, such as power consumption or memory footprint, affects the success of the App. This is why it is so important to ensure that the Android system is optimized, runs smoothly, and doesn't cause problems during development. We don't need to talk about efficient programming here, because we don't care if your code stands up to testing. Even efficient code takes time to run. In this article, we'll talk about how to shorten the runtime as much as possible, and how to develop App to your liking.

Use threads efficiently

Tip 1: How do I cancel some thread actions in the background

We know that App runs all operations in the main thread (UI thread) by default, so App's response speed is affected. Can cause a program to stall, die, or even cause a system error.
To speed up response times, time-consuming operations (such as network requests, database operations, or complex computations) need to be moved from the main thread to a separate thread. The most efficient way to do this is at the class level 1, where 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 through a worker thread (Intent).
The following limitations should be noted when using IntentService:

This class does not pass information to UI. If you want to show the user the result of processing, please use Activity.
Only one request can be processed at a time;
Every 1 processing request process can not be interrupted;

Tip 2: How do I keep the response from happening ANR

Removing time-consuming operations from UI threads also prevents user operations from having the system unresponsive (ANR) dialog box. All you need to do is inherit from AsyncTask to create a background worker thread and implement the doInBackground() method.

Another option is to create your own Thread class or HandlerThread class. Note that this also slows App down because the default thread priority is the same as the main thread priority unless you explicitly set the thread priority.

Recommendation 3: How do You initialize a query operation in a thread

While the query operation is being processed in the background, the presentation data is not instantaneous, but you can use the CursorLoader object to speed things up, and this action will not affect the interaction between Activity and the user.
Using this object, your App will initialize a separate background thread for ContentProvider to query, and when the query is finished, it will return the result to the Activity that called the query.

Tip 4: Other areas that need attention

Use StrictMode to check possible potentially time-consuming operations in UI threads;
Use 1 specific tool such as Systrace or Traceview to find bottlenecks in your application;
Use the progress bar to show the operation progress to the user;
(4) If the initialization operation is time-consuming, please show a welcome interface.

Optimize the battery life of the equipment

Don't blame the user for uninstalling your app if it USES a lot of power. For battery use, the main power cost is as follows:

Often wake up the program when updating data;
Use EDGE or 3G to transmit data;
Text data conversion, non-ES79en regular expression operation.

Tip 5: How do you optimize your network

(1) If there is no network connection, please let your application skip network operation; Update data only if there is a network connection and no roaming;
(2) Select compatible data format and convert all requests containing text data and base 2 data into base 2 data format requests;
Use efficient conversion tools, more consider the use of streaming conversion tools, less tree conversion tools;
(4) In order to faster user experience, please reduce repeated access to the server operation;
If possible, please use GZIP library of framework to compress text data to use CPU resources efficiently.

Tip 6: How do you optimize how your application works on the front end

If wakelocks is considered, try to set it to the minimum level;
To prevent potential POWER consumption due to bug, specify a specific timeout period;
Enable android:keepScreenOn attribute;
In addition to the GC operation of the system, consider more manual recycling Java objects, such as XmlPullParserFactory and BitmapFactory. There are regular expressions of Matcher. reset(newString) operation, ES109en. setLength(0) operation;
(5) Pay attention to the synchronization problem, although it is safe in the main thread;
In Listview to use more reuse strategy;
If allowed, use rough network positioning rather than GPS, compared to 1 GPS (25s * 140 mA), 1 es1120en (2s * 180mA);
Today ensures that the GPS location update operation is cancelled, as this update will continue in onPause() as well. When all applications log out, the user can re-enable GPS in system Settings without wasting power.
(9) Heavy use of low precision variables in heavy mathematical operations and caching of variable values when performing DPI tasks with DisplayMetrics;

Tip 7: How to optimize the application of your work in the foreground

Please make sure that the service life cycle is short, because each process needs the memory of 2MB, and will restart when the foreground program needs the memory;
Keep the memory usage not too big;
If you want to use the update every 30 minutes, please do it when the device is in the wake state;
Service in pull or sleep state is not good, this is why at the end of the service to use AlarmManager or configuration attribute stopSelf().

Recommendation 8: Other precautions

(1) Check the battery state and network state before the overall update, and wait for the best state for large installation and replacement operation;
Let the user see the electricity situation, such as update cycle, background operation when;

Achieve low memory footprint UI

Tip 9: How do I find layout display issues

When we create UI for the layout alone, we are creating App for memory abuse, which has a nasty latency in UI. To achieve a smooth, low-memory UI, the first step is to search your application for potential bottleneck layouts. Use the Hierarchy Viewer Tool tool that comes with Android SDK/tools/.
Another great tool is Lint, which scans the application's source code for possible bug and optimizes the results for controls.

Tip 10: How to solve the problem

If the layout display turns out to be problematic, you can consider simplifying the layout structure. You can convert the LinearLayout type to the RelativeLayout type to reduce the hierarchy of the layout.

Be more perfect and keep optimizing

While each of the Suggestions above may seem like a small improvement, if it becomes part of your everyday code, you will see unexpected results. To make Google Play see more outstanding, smooth, faster and more power-saving applications, we will take a step towards the perfect goal of Android.

I hope this article has been helpful in Android programming.


Related articles: