Android provides asynctask, but in fact the asynctask problem is even more serious, thread only occurs when the run function does not end the memory leak problem, but asynctask internal implementation mechanism is the use of Threadpoolexcutor , the life cycle of the thread object produced by this class is indeterminate and is beyond the control of the application, so if Asynctask is the inner class of activity, it is more prone to memory leaks.
The general application is to obtain the data from the server, and then through the extreme interface style, the data clear and clear display to the user.
Then it can be divided into these two pieces:
1. Interface UI to pursue the ultimate2. function
2.1 Getting Data: Mainly communication with the server, then it involves network programming:
2.1.1URLConnection
2.1.2HttpURLConnection (Post get)
2.1.3Socket
2.1.4HttpClient (Post get)
2.1.5WebService (self-assembled request XML data, using Open source jar package ksoap-android-)
2.2 Asynchronous data acquisition when network communication is in use
2.2.1Asyntask Android packaged asynchronous data acquisition, including three methods
Doinbackground executing async methods in a child thread
Onprogressupdate the method that is executed after data acquisition, where the UI interface can be updated in the main thread
OnPostExecute methods that are executed in the main thread before the asynchronous method can be performed with friendly hints
2.2.2Encapsulate a task class (child thread) thread pool it needs to maintain a task list and prioritize tasks.
2.3 Android Optimizer most common is the ListView's Big Data optimization picture optimized for access network optimization
2.3.1Optimization principle: Data delay loading local cache in batches
2.3.2ListView Data Optimization Multiplexing Contentview
Create a static class Viewholder
Batch load slide monitor or button to show more data drag down display
2.3.3ListView picture Optimized for asynchronous loading
Local cache (level two cache memory (soft reference implementation), SD card)
Do not show pictures when sliding quickly
Divided into core thread pool and common thread pool, download pictures and other time-consuming tasks placed in the normal thread pool
2.3.4Super Big Fat Bitmap
Timely destruction (the activity of the OnDestroy will be bitmap recycled,
Recycling immediately after being used by the UI component throws RuntimeException:
CANVAS:TRYINGTOUSEARECYCLEDBITMAPANDROID.GRAPHICS.BITMAP)
Set a certain sample rate (there is no need to sample a developer-supplied picture
For users to upload or third-party size of the uncontrolled picture, can be sampled to reduce the memory of the image),
Return the image from the service side, suggest that the size of the picture is also smart use soft reference drawable corresponding to Resid resources,
Bitmap corresponds to other resources of any type of picture, if not obtained (for example, if the file does not exist, or if the file runs OutOfMemory exception),
There should be a corresponding default picture (the default image is placed in the APK, obtained by RESID);
2.3.5Drawable
The image that the UI component needs to use is the APK package that comes with it,
Then use Setimageresource or setbackgroundresource, not according to ResourceID
Note:Get (Getresources (), R.drawable.btn_achievement_normal) This method is converted to drawable by Resid,
The issue of recycling needs to be considered, and if drawable is an object private object, it will certainly not free memory until the object is destroyed.
2.3.6Access Network optimization
Set the time-out period and send the data by compression2.3.7Memory-Optimized, static is a keyword in Java that, when used to decorate a member variable, belongs to that class, not an instance of that class. So the variable with static modification, its life cycle is very long.
Optimization method: Centrally manage these Static constants in a project
Try to avoid the use of static member variables,
Use SoftReference or weakreference instead of strong references
Try to avoid writing thread-internal classes in an activity:
The thread is the inner class of activity, so a reference to the activity is kept in thread, and the thread is not destroyed when the run function is not finished.
So the old activity that it refers to is not destroyed, and when the activity loads a lot of resources, it's easy to have a memory leak without releasing it.
The problem.
Android provides asynctask, but in fact the asynctask problem is even more serious, thread only occurs when the run function does not end the memory leak problem, but asynctask internal implementation mechanism is to use the
Threadpoolexcutor, the life cycle of the thread object produced by this class is indeterminate and is beyond the control of the application,
Therefore, if Asynctask is the internal class of activity, the problem of memory leaks is more likely.
Then write a thread class yourself to manage these tasks.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Summary of network programming and common function optimization in Android development