Android service description

Source: Internet
Author: User

The Establishment of The Open Mobile Alliance and the launch of the Android service are significant changes to the current situation. Prior to the initial benefits, it still requires a lot of patience and high investment. Google will continue to work hard, make these services better and add more attractive features, applications, and services.

1. The Service in the Android Service is in the same thread as the caller, so it is necessary to open a new thread in the Service for time-consuming operations.
2. In Android services, we mainly implement functions such as onCreate, onStart, onDestroy, onBind, and onUnBind to implement the functions we need.

Simple calls can be called using Context. startService In the caller object. Intent is used as the parameter. Of course, Intent search matches the target in the same way as in Intent usage.

Let's look at a routine:

 
 
  1. package test.pHello;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.ServiceConnection;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.os.IBinder;  
  11. import android.view.Menu;  
  12. import android.view.MenuItem;  
  13. import android.widget.TextView;  
  14.  
  15. public class HelloActivity extends Activity {   
  16.    
  17.  ITestService mService = null;  
  18.  ServiceConnection sconnection = new ServiceConnection()  
  19.  {  
  20.   public void onServiceConnected(ComponentName name, IBinder service)  
  21.   {  
  22.    mService  = (ITestService)service;  
  23.    if (mService != null)  
  24.    {  
  25.     mService.showName();  
  26.    }  
  27.   }  
  28.  
  29.   public void onServiceDisconnected(ComponentName name)   
  30.   {     
  31.      
  32.   }  
  33.  };  
  34.  @Override  
  35.  public boolean onCreateOptionsMenu(Menu menu) {  
  36.   // TODO Auto-generated method stub  
  37.   super.onCreateOptionsMenu(menu);  
  38.   menu.add(0, Menu.FIRST+1, 1, "OpenActivity");  
  39.   menu.add(0, Menu.FIRST+2, 2, "StartService");  
  40.   menu.add(0, Menu.FIRST+3, 3, "StopService");  
  41.   menu.add(0, Menu.FIRST+4, 4, "BindService");  
  42.   return true;  
  43.  }  
  44.  
  45.  @Override  
  46.  public boolean onOptionsItemSelected(MenuItem item) {  
  47.   // TODO Auto-generated method stub  
  48.   super.onOptionsItemSelected(item);  
  49.   switch(item.getItemId())  
  50.   {  
  51.   case Menu.FIRST + 1:  
  52.   {  
  53.    this.setTitle("Switch Activity");  
  54.    Intent i = new Intent();     
  55.    i.setAction("test_action");    
  56.    if (Tools.isIntentAvailable(this,i))  
  57.     this.startActivity(i);  
  58.    else  
  59.     this.setTitle("the Intent is unavailable!!!");  
  60.    break;  
  61.   }  
  62.   case Menu.FIRST + 2:  
  63.   {  
  64.    this.setTitle("Start Service");  
  65.    //Intent i = new Intent(this, TestService.class);  
  66.    Intent i = new Intent();  
  67.    i.setAction("start_service");  
  68.    this.startService(i);  
  69.    break;  
  70.   }  
  71.   case Menu.FIRST + 3:  
  72.   {  
  73.    this.setTitle("Stop Service");  
  74.    Intent i = new Intent(this, TestService.class);  
  75.    this.stopService(i);  
  76.    break;  
  77.   }  
  78.   case Menu.FIRST + 4:  
  79.   {  
  80.    this.setTitle("Bind Service!");  
  81.    Intent i = new Intent(this, TestService.class);  
  82.    this.bindService(i, this.sconnection, Context.BIND_AUTO_CREATE);  
  83.      
  84.    break;  
  85.   }  
  86.   }  
  87.   return true;  
  88.  }  
  89.  
  90.  @Override  
  91.     public void onCreate(Bundle savedInstanceState) {  
  92.         super.onCreate(savedInstanceState);         
  93.         this.setContentView(R.layout.main);     
  94.     }  
  95. }  

Compile and execute. You will find that onCreate is executed first and then onBind is executed. When Context. bindService of the caller returns, OnConnected of ServiceConnection is not executed immediately. Remote binding of Android services: The preceding binding is performed when the caller and Service are in the same application. If the caller and Service are in different programs, the call method is in another situation. Let's take a look.

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.