10月9日Android学习笔记:活动与服务之间的通信

时间:2023-03-09 13:05:35
10月9日Android学习笔记:活动与服务之间的通信

最近在照着《第一行代码》这本书来学安卓,顺便记下笔记。主要的内容是Android中服务的第二种启动方式,通过活动绑定服务来启动服务,实现活动与服务之间的通信。

一. 首先创建一个服务类

 public class MyService extends Service{

     private DownloadBinder mBinder=new DownloadBinder();

     class DownloadBinder extends Binder{
public void startDownload(){
Log.d("MyService", "start download");
} public int getProgress(){
Log.d("MyService", "getProgress executed");
return 0;
}
} public IBinder onBind(Intent intent){
return mBinder;
} public void onCreate(){
super.onCreate();
Log.d("MyService", "onCreate executed");
} public int onStartCommand(Intent intent, int flags,int startId){
Log.d("MyService", "onStartCommand executed");
return super.onStartCommand(intent, flags, startId);
} public void onDestroy(){
Log.d("MyService", "onDestroy executed");
super.onDestroy();
}
}

在MyService里重写onCreate(), onStartCommand(), onDestroy()和onBind()方法,这里重点要说下的是onBind()方法,它会在服务被绑定时执行,并且返回一个实现了IBinder接口的实例。这里在MyService中新建了DownloadBinder继承Binder并将其作为onBind()方法的返回值。

二. 在活动的布局中加入两个按钮用于绑定服务和解除绑定服务

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.servicetest.MainActivity" > <Button
android:id="@+id/bind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bind Service"/> <Button
android:id="@+id/unbind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Unbind Service"/> </LinearLayout>

之后修改MainActicity中的代码

public class MainActivity extends Activity implements OnClickListener{

    private Button bindService;
private Button unbindService;
private MyService.DownloadBinder downloadBinder;
private ServiceConnection connection=new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub } @Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
downloadBinder=(MyService.DownloadBinder)service;
downloadBinder.startDownload();
downloadBinder.getProgress();
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindService=(Button)findViewById(R.id.bind_service);
unbindService=(Button)findViewById(R.id.unbind_service);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
} //启动和停止服务
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {case R.id.bind_service:
Intent bindIntent=new Intent(this,MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
break;
case R.id.unbind_service:
unbindService(connection);
break;
default:
break;
}
} }

这里的要点:1. ServiceConnection类         2. Activity中的bindService和unbindService方法

自己的总结:Activity调用自己的bindService方法去绑定服务Service,这个方法里需要3个参数。既然是Activity和Service的绑定,那么这两个类的对象是一定要的,可以将这两个用一个Intent传入。还需要一个对象来代表两者之间的连接,所以第二个参数需要一个ServiceConnection的实例,这个类有两个方法:onServiceConnected和onServiceDisconnected分别在Activity绑定Service和接触绑定时执行。onServiceConnected(ComponentName name, IBinder service)这个方法里的第二个参数应该就是Service在绑定时执行onbind方法时返回的IBinder。

最后控制台的输出:

10月9日Android学习笔记:活动与服务之间的通信