详细描述Android服务解说

移动开发 Android
Android服务中有几个重要的组件,其中之一就是Service,这是没有UI的组件,可以做为后台的服务,当然可以使用Intent来启动。

开放手机联盟的成立和 Android服务 的推出是对现状的重大改变,在带来初步效益之前,还需要不小的耐心和高昂的投入,谷歌将继续努力,让这些服务变得更好,同时也将添加更有吸引力的特性、应用和服务。

一,Android服务中的Service与调用者在同一线程,所以要是耗时的操作要在Service中新开线程。
二,Android的Service中,主要是实现其onCreate,onStart, onDestroy,onBind,onUnBind几个函数,来实现我们所需要的功能。

简单的调可以在调用者对象中使用Context.startService来调用,以Intent为参数,当然,Intent搜索,匹配目标的方式与以前在《Intent使用》中方式一样。

下面来看一段例程:

  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. }  

编译执行,你会发现,是先执行onCreate,然后再执行onBind,在调用者的Context.bindService返回时,ServiceConnection的OnConnected并没有马上被执行。Android服务远程绑定:上述绑定是在调用者与Service在同一个应用程序中的情况,如果分处在不同的程序中,那么,调用方式又是一另一种情况。我们来看一下。

【编辑推荐】

  1. Android应用程序组建原理深入剖析
  2. Android SMS短信服务相关概念简述
  3. PythonAndroid数据库相关代码解读
  4. PythonAndroid安装卸载程序具体操作方法解析
  5. Android应用程序的四个关键点
责任编辑:chenqingxiang 来源: 清华大学出版社
相关推荐

2009-10-13 17:16:40

VB.NET Web服

2009-09-14 14:58:52

LINQ to XML

2009-09-14 13:14:49

LINQ序列

2009-09-24 16:19:53

Hibernate应用

2009-09-14 16:33:55

LINQ To XML

2009-09-25 14:28:40

Hibernate S

2009-11-18 11:14:49

2009-09-25 11:04:32

Hibernate3实

2009-08-10 16:40:03

C#索引器

2009-10-15 14:59:45

网络布线光纤技术

2009-09-08 11:09:39

LINQ数据源

2010-04-09 17:45:06

Oracle索引

2009-09-07 15:15:43

2009-08-26 15:53:48

C#扩展方法

2009-10-10 10:04:50

RHEL合法使用

2009-08-27 15:17:40

C# const变量

2010-09-08 15:10:48

2011-11-02 09:29:42

存储虚拟化虚拟化

2009-10-12 12:54:58

VB.NET声明API

2009-10-16 11:02:40

VB调用动态连接库
点赞
收藏

51CTO技术栈公众号