. Net programmers play with Android development-Android IntentService

Source: Internet
Author: User

. Net programmers play with Android development-Android IntentService

Intentservice is also a service in Android. It inherits from service, but is different from servcie. It starts a new thread to process all requests and does not need to wait for processing by the UI, the onStartCommand method sends all requests to the work queue, and then the work queue sends them to the onHandlerIntent for processing.

1. By default, interservice enables a new thread to process tasks. By default, the service is executed in the ui thread.

2. interservice creates a work queue to process the received tasks. Only the current task is completed before the next task can be processed. Otherwise, the next task remains in the waiting state.

3. After all request tasks are processed, the system automatically stops the service and does not need to manually stop stopselft.

The following example shows that intentservice continuously accepts externally transmitted messages and processes them in order. First, it accepts the messages in onStartCommand and then processes them in onHandleIntent.

1. Create an intentservice

 

Package com. example. helloword; import android. app. intentService; import android. content. intent; import android. OS. bundle; import android. OS. handler; import android. util. log; import android. widget. toast; public class ReceiveIntentService extends IntentService {String receivemsg; private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {Toast. makeText (ReceiveIntentService. this, receives the message: + receivemsg, Toast. LENGTH_LONG ). show () ;}}; public ReceiveIntentService () {super (1111); // TODO Auto-generated constructor stub} public ReceiveIntentService (String name) {super (name ); // TODO Auto-generated constructor stub} @ Override public int onStartCommand (Intent intent, int flags, int startId) {Bundle bundle = intent. getExtras (); receivemsg = bundle. getString (para); return super. onStartCommand (intent, flags, startId) ;}@ Overrideprotected void onHandleIntent (Intent intent) {// TODO Auto-generated method stub try {Thread. sleep (5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} handler. sendEmptyMessage (0 );}}

 

 

2. continuously send messages to intentservice through activity

Layout File

 

 
     
              
  
  
 

Background IntentActivity

 

Package com. example. helloword; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; public class IntentActivity extends Activity {private Button btnpost; // start the service private EditText etmsg; Intent intent; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. intentlayout); btnpost = (Button) findViewById (R. id. btnpostmsg); etmsg = (EditText) findViewById (R. id. etpostmsg); btnpost. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub intent = new Intent (com. example. helloword. receiveIntentService); Bundle bundle = new Bundle (); bundle. putString (para, etmsg. getText (). toString (); intent. putExtras (bundle); startService (intent );}});}}

 

 

 

                               
                                 
              
  
                   
      
                      
       
   
    
  


 

 

??

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.