转载自:http://www.cnblogs.com/onlylittlegod/archive/2011/05/15/2046652.html

最近学习了一下Android里面的Service的应用,在BindService部分小卡了一下,主要是开始没有彻底理解为什么要这么实现。

BindService和Started Service都是Service,有什么地方不一样呢:

1. Started Service中使用StartService()方法来进行方法的调用,调用者和服务之间没有联系,即使调用者退出了,服务依然在进行【onCreate()- >onStartCommand()->startService()->onDestroy()】,注意其中没有onStart(),主要是被onStartCommand()方法给取代了,onStart方法不推荐使用了。

2. BindService中使用bindService()方法来绑定服务,调用者和绑定者绑在一起,调用者一旦退出服务也就终止了【onCreate()->onBind()->onUnbind()->onDestroy()】。

调用者Activity:

MainActivity
 1 MainAcitvity 2  3 package com.zys.service; 4  5 import com.zys.service.BindService.MyBinder; 6  7 import android.R.bool; 8 import android.app.Activity; 9 import android.content.ComponentName;10 import android.content.Context;11 import android.content.Intent;12 import android.content.ServiceConnection;13 import android.os.Bundle;14 import android.os.IBinder;15 import android.view.View;16 import android.view.View.OnClickListener;17 import android.widget.Button;18 19 public class MainActivity extends Activity {20     private Button startBtn;21     private Button stopBtn;22     private boolean flag;23     /** Called when the activity is first created. */24     @Override25     public void onCreate(Bundle savedInstanceState) {26         super.onCreate(savedInstanceState);27         setContentView(R.layout.main);28         flag = false;29         //设置30         startBtn = (Button)this.findViewById(R.id.startBtn);31         stopBtn = (Button)this.findViewById(R.id.stopBtn);32         startBtn.setOnClickListener(listener);33         stopBtn.setOnClickListener(listener);34     }35     36     private OnClickListener listener = new OnClickListener() {37         38         @Override39         public void onClick(View v) {40             // TODO Auto-generated method stub41             switch (v.getId()) {42             case R.id.startBtn:43                 bindService();44                 break;45             case R.id.stopBtn:46                 unBind();47                 break;48             default:49                 break;50             }51         }52         53         54     };55     56     private void bindService(){57         Intent intent = new Intent(MainActivity.this,BindService.class);58         bindService(intent, conn, Context.BIND_AUTO_CREATE);59     }60     61     private void unBind(){62         if(flag == true){63             unbindService(conn);64             flag = false;65         }66     }67     68     private ServiceConnection conn = new ServiceConnection() {69         70         @Override71         public void onServiceDisconnected(ComponentName name) {72             // TODO Auto-generated method stub73             74         }75         76         @Override77         public void onServiceConnected(ComponentName name, IBinder service) {78             // TODO Auto-generated method stub79             MyBinder binder = (MyBinder)service;80             BindService bindService = binder.getService();81             bindService.MyMethod();82             flag = true;83         }84     };85     86 }

服务BindService:

BindService
BindServicepackage com.zys.service;import java.io.FileDescriptor;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.os.IInterface;import android.os.Parcel;import android.os.RemoteException;import android.util.Log;public class BindService extends Service {    private static final String TAG = "BindService";    public void MyMethod(){        Log.i(TAG, "BindService-->MyMethod()");    }        @Override    public IBinder onBind(Intent intent) {        // TODO Auto-generated method stub        return myBinder;    }        public class MyBinder extends Binder{                public BindService getService(){            return BindService.this;        }    }        private MyBinder myBinder = new MyBinder();}

  由于Android 中的Service使用了onBind 的方法去绑定服务,返回一个Ibinder对象进行操作,而我们要获取具体的Service方法的内容的时候,我们需要Ibinder对象返回具体的Service对象才能操作,所以说具体的Service对象必须首先实现Binder对象,这个样子的话我们才能利用bindService的方法对Service进行绑定,获取Binder对象之后获取具体的Service对象,然后才获取Service中的方法等等。所以我们需要注意的是bindService的方式去绑定服务获取的必定是实现了Binder的对象,所以这是我们必须使用Binder的方式去获取Service的方式而不是直接使用Service的类,这个是Android内部实现所约束的。

方法过程如下:

Intent intent = new Intent(MainActivity.this,BindService.class)->新建了BindService对象->新建了MyBinder对象

->bindService(intent, conn, Context.BIND_AUTO_CREATE);->onBind()函数 -----传递MyBinder对象------->onServiceConnected()--> 通过传递的Binder对象获取刚刚和Binder对象对应的BindService 对象  -->调用Service中定义的方法。

  这个其中必须通过Binder对象,因为是通过Binder对象来传递的,通过Binder对象获取Service对象,然后获取所需的服务,所以Service必须实现Binder,以便传递和使用。

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. 类和 Json对象
  3. Python list sort方法的具体使用
  4. python list.sort()根据多个关键字排序的方法实现
  5. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  6. android上一些方法的区别和用法的注意事项
  7. Android屏幕分辨率正确获取及PX,DPI,DP,SP等的对应关系
  8. android实现字体闪烁动画的方法
  9. Android中dispatchDraw分析

随机推荐

  1. Android:GridView+AbsoluteLayout作一个
  2. android startService onStartCommand 多
  3. android 布局属性
  4. Android:GridView+AbsoluteLayout作一个
  5. android:onClick事件
  6. Android---inputType参数类型
  7. 常见Theme
  8. android android:taskAffinity 详解
  9. Android附带Theme总结
  10. Android(安卓)图片文字叠加设置