Android handler (100 million interview requests) and androidhandler

Source: Internet
Author: User

Android handler (100 million interview requests) and androidhandler

Handler is called "message handler" in Android and is often used in multiple threads.

Internal implementation principle of handler

 

Handler implementation mechanism:
1. The Message object indicates a Message to be transmitted. A Message pool is implemented using the Linked List Data Structure internally for reuse,
Avoid a large number of creation of message objects, resulting in a waste of memory
2. MessageQueue object: Message Queue for storing message objects. FIFO Principle
3. The logoff object is responsible for managing the message queue of the current thread.
4. the handler object is responsible for pushing messages to the message queue, and receiving messages that logoff retrieves from the message queue.

 

Handler Memory leakage (activity has exited, but handler has not exited, which may cause memory leakage)
1. When defining an internal class, it will have reference to the external class object by default. Therefore, it is recommended to define a static internal class when using an internal class.
2. Weak references: strong references → soft references → weak references

 

The following code obtains the network image to describe the basic use of handler.

Package com. example. uri; import java. io. IOException; import java. io. inputStream; import java. lang. ref. weakReference; import java.net. malformedURLException; import java.net. URL; import android.net. uri; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. app. activity; import android. content. context; import android. content. intent; import android. graphics. bitmap; import and Roid. graphics. bitmapFactory; import android. view. menu; import android. view. view; import android. widget. imageView;/***** network access operations must be completed in the work thread **/public class MainActivity extends Activity {private static final int LOADSUCCESS = 0x1; private static ImageView iv; private final myhandler handler = new myhandler (this); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstan CeState); setContentView (R. layout. activity_main); iv = (ImageView) findViewById (R. id. imageView1);} private static class myhandler extends Handler {private final WeakReference <MainActivity> weakReference; public myhandler (MainActivity mainActivity) {weakReference = new WeakReference <MainActivity> (mainActivity );} public void handleMessage (Message msg) {MainActivity mainActivity = weakReference. get (); I F (mainActivity! = Null) {switch (msg. what) {case LOADSUCCESS: MainActivity. iv. setImageBitmap (Bitmap) msg. obj); break ;}}} public void geturl (View v) {/* Intent intent = new Intent (Intent. ACTION_VIEW, Uri. parse ("http://www.baidu.com"); startActivity (intent); */new Thread (new Runnable () {@ Override public void run () {try {URL url = new URL ("http://img2.3lian.com/img2007/10/28/123.jpg"); InputStream in = url. openStream (); Bitmap bitmap = BitmapFactory. decodeStream (in); Message message = handler. obtainMessage (LOADSUCCESS, bitmap); handler. sendMessage (message);} catch (MalformedURLException e) {// catch Block e automatically generated by TODO. printStackTrace ();} catch (IOException e) {// catch Block e automatically generated by TODO. printStackTrace ();}}}). start ();}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.