(1)创建一个Service:
package com.syz.bindservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class Bindservice extends Service {
private static final String TAG = "Bindservice";
private MyService binder=new MyService();
public class MyService extends Binder //其中Binder是实现IBinder接口的:public class Binder implements IBinder
{ //,通过继承来实现IBinder
public Bindservice getservice()
{ //返回Bindservice的对象,供后面的Activity调用Service使用
return Bindservice.this ;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub //返回IBinder的对象
return binder;
}
public void MyMethod()
{
Log.i(TAG, "MyMethod()");
}
}
//此Service的理解:因为IBinder是接口,所以需要定义一个类实现这个接口,即通过定义MyService继承Binder类来实现了IBinder的接口
//。在MyService中实现一个方法返回当前的Bindservice,然后通过onBind将该Bindservice
//传递给后面Activity中使用,这样在Activity中用户就可以通过Bindservice调用其中的Service的方法,比如:MyMethod()
(2)在布局文件中定义两个Button:
<Button
android:id="@+id/startbindservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<Button
android:id="@+id/stoptbindservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/startbindservice"
android:text="@string/hello_world1"
/>
(3)在Activity中与Service绑定:
package com.syz.bindservice;
import com.syz.bindservice.Bindservice.MyService;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private static final String TAG = "Bindservice";
private Button startbindservice=null;
private Button stopbindservice=null;
private boolean f=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startbindservice=(Button)findViewById(R.id.startbindservice);
stopbindservice=(Button)findViewById(R.id.stoptbindservice);
startbindservice.setOnClickListener(listener);
stopbindservice.setOnClickListener(listener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private OnClickListener listener =new OnClickListener()
{
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startbindservice:
bindService(); //实现service与Activity的绑定,把其实现放在外面,有利于程序的布局,
break;
case R.id.stoptbindservice:
unbindService();
break;
default:
break;
}
}
};
private void bindService() { //主要实现bindService(service, conn, flags),其中的参数意义参见Service学习(一)
Log.i(TAG, "bindService()创建连接");
Intent intent=new Intent();
intent.setClass(MainActivity.this, Bindservice.class);
bindService(intent, conn, BIND_AUTO_CREATE); //conn是ServiceConnection对象,因此要将其实现,并且要实现里面的方法
}
private void unbindService() {
Log.i(TAG, "unbindService断开连接");
if(f) //判断是否连接成功,因为只有连接成功了,才有断开之说
{
unbindService(conn);
}
}
private ServiceConnection conn=new ServiceConnection()
{
@Override//当Service与Activity连接成功回调该方法
public void onServiceConnected(ComponentName name, IBinder binder) { //binder就是前面的Service返回的对象
Log.i(TAG, "onServiceConnected回调");
MyService myservice=(MyService)binder; //接收从Service返回的binder对象,是MyService类型
Bindservice bindservice=myservice.getservice(); //通过binder中的方法返回Bindservice类型的对象
bindservice.MyMethod(); //通过bindservice调用其中的方法
f=true; //连接成功就将标志f置为true
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "onServiceDisconnected");
}
};
}
注:我测试下了,正常断开连接时一般不会回调onServiceDisconnected方法的,除非出现异常断开时会回调onServiceDisconnected方法