Measure the test taker's understanding and usage of logoff, HandlerThread, and IntentService in android.

Source: Internet
Author: User

Message Queue Processing Model

Container saves events and queues

Roles involved:

Class Functions

MessageQueue stores messages

Logoff round-robin message queue to retrieve messages

Thread processes messages

Message

Handler sends messages to the current thread in other threads


1. logoff examples and descriptions

Determined by Logoff

The thread on which the logoff object is created,

The thread in which the handleMessage method of Handler is executed

Public class MainActivity extends Activity {private Handler mHandler; int what = 0; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {mHandler. sendEmptyMessage (what ++); // The main Thread sends new Thread ("aaa") {public void run () {mHandler. sendEmptyMessage (what ++); // "aaa" subthread sending }}. start () ;}}); new Thread ("custom message queue") {// a single Thread processes messages sent from all threads, public void run () {loid. prepare (); // view rows 80 and 189 in prepare (), and create messageQueue, low.mhandler = new Handler (low.mhandler. mylogoff () // logoff. mylode () gets the looper {// the thread in which the handler processes the Message, depending on the thread in which the Looper is created @ Override public void handleMessage (Message msg) {String content = "current Thread:" + Thread. currentThread (). getName () + "msg:" + msg. what; Toast. makeText (MainActivity. this, content, Toast. LENGTH_SHORT ). show () ;}}; Looper. loop (); // logoff start round robin queue }}. start () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}



2. HandlerThread

Encapsulated Thread, logoff, and MessageQueue

Public class MainActivity extends Activity {private Handler mHandler; int what = 0; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {mHandler. sendEmptyMessage (what ++); // The main Thread sends new Thread ("aaa") {public void run () {mHandler. sendEmptyMessage (what ++); // "aaa" subthread sending }}. start () ;}}); // because logoff has been encapsulated in HandlerThread. prepare () and logoff. loop (); HandlerThread thread = new HandlerThread ("custom message queue"); thread. start (); // thread. if getLooper () is replaced with Looper. mylofter () is to get the main thread low.mhandler = new Handler (thread. getLooper () {// the thread in which the handler processes the Message depends on the thread in which the logoff creates @ Override public void handleMessage (Message msg) {String content = "current thread: "+ Thread. currentThread (). getName () + "msg:" + msg. what; Toast. makeText (MainActivity. this, content, Toast. LENGTH_SHORT ). show () ;};}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}



3. IntentService

Thread, logoff, MessageQueue, Handler, Message


Explicitly starting the service in MainActivity

Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// start MyService and pass the value to Intent service = new Intent (MainActivity. this, MyService. class); service. putExtra ("msg", "aaa"); startService (service );}});}}


MyService inherits the IntentService and receives the value from MainActivity.

Public class MyService extends IntentService {// encapsulate handleMessage (); 64 rows // write HandlerThread thread = new HandlerThread ("IntentService [" + mName + "]"); // thread. start (); public MyService () {// IntentService has a constructor with parameters. Line 75 super ("Message Queue IntentService") ;}@ Override public IBinder onBind (Intent intent) {// TODO: Return the communication channel to the service. throw new UnsupportedOperationException ("Not yet implemented");} @ Override protected void onHandleIntent (Intent intent) {String extra = intent. getStringExtra ("msg"); String content = "current Thread:" + Thread. currentThread (). getName () + "msg:" + extra; Log. e ("onHandleIntent", content );}}


This article is from the "wangcuijing" blog, please be sure to keep this source http://wangcuijing.blog.51cto.com/7233352/1290537

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.