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

成都创新互联公司主要从事成都网站建设、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务双鸭山,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
一,Android服务中的Service与调用者在同一线程,所以要是耗时的操作要在Service中新开线程。
 二,Android的Service中,主要是实现其onCreate,onStart, onDestroy,onBind,onUnBind几个函数,来实现我们所需要的功能。
简单的调可以在调用者对象中使用Context.startService来调用,以Intent为参数,当然,Intent搜索,匹配目标的方式与以前在《Intent使用》中方式一样。
下面来看一段例程:
- package test.pHello;
 - import android.app.Activity;
 - import android.content.ComponentName;
 - import android.content.Context;
 - import android.content.Intent;
 - import android.content.ServiceConnection;
 - import android.net.Uri;
 - import android.os.Bundle;
 - import android.os.IBinder;
 - import android.view.Menu;
 - import android.view.MenuItem;
 - import android.widget.TextView;
 - public class HelloActivity extends Activity {
 - ITestService mService = null;
 - ServiceConnection sconnection = new ServiceConnection()
 - {
 - public void onServiceConnected(ComponentName name, IBinder service)
 - {
 - mService = (ITestService)service;
 - if (mService != null)
 - {
 - mService.showName();
 - }
 - }
 - public void onServiceDisconnected(ComponentName name)
 - {
 - }
 - };
 - @Override
 - public boolean onCreateOptionsMenu(Menu menu) {
 - // TODO Auto-generated method stub
 - super.onCreateOptionsMenu(menu);
 - menu.add(0, Menu.FIRST+1, 1, "OpenActivity");
 - menu.add(0, Menu.FIRST+2, 2, "StartService");
 - menu.add(0, Menu.FIRST+3, 3, "StopService");
 - menu.add(0, Menu.FIRST+4, 4, "BindService");
 - return true;
 - }
 - @Override
 - public boolean onOptionsItemSelected(MenuItem item) {
 - // TODO Auto-generated method stub
 - super.onOptionsItemSelected(item);
 - switch(item.getItemId())
 - {
 - case Menu.FIRST + 1:
 - {
 - this.setTitle("Switch Activity");
 - Intent i = new Intent();
 - i.setAction("test_action");
 - if (Tools.isIntentAvailable(this,i))
 - this.startActivity(i);
 - else
 - this.setTitle("the Intent is unavailable!!!");
 - break;
 - }
 - case Menu.FIRST + 2:
 - {
 - this.setTitle("Start Service");
 - //Intent i = new Intent(this, TestService.class);
 - Intent i = new Intent();
 - i.setAction("start_service");
 - this.startService(i);
 - break;
 - }
 - case Menu.FIRST + 3:
 - {
 - this.setTitle("Stop Service");
 - Intent i = new Intent(this, TestService.class);
 - this.stopService(i);
 - break;
 - }
 - case Menu.FIRST + 4:
 - {
 - this.setTitle("Bind Service!");
 - Intent i = new Intent(this, TestService.class);
 - this.bindService(i, this.sconnection, Context.BIND_AUTO_CREATE);
 - break;
 - }
 - }
 - return true;
 - }
 - @Override
 - public void onCreate(Bundle savedInstanceState) {
 - super.onCreate(savedInstanceState);
 - this.setContentView(R.layout.main);
 - }
 - }
 
编译执行,你会发现,是先执行onCreate,然后再执行onBind,在调用者的Context.bindService返回时,ServiceConnection的OnConnected并没有马上被执行。Android服务远程绑定:上述绑定是在调用者与Service在同一个应用程序中的情况,如果分处在不同的程序中,那么,调用方式又是一另一种情况。我们来看一下。