Service和Activity通信
我们学习了Service的基本用法,启动Service之后,就可以在onCreate()或onStartCommand()方法里去执行一些具体的逻辑了。不过这样的话Service和Activity的关系并不大,只是Activity通知了Service一下:“你可以启动了。”然后Service就去忙自己的事情了。那么有没有什么办法能让它们俩的关联更多一些呢?比如说在Activity中可以指定让Service去执行什么任务。当然可以,只需要让Activity和Service建立关联就好了。
观察MyService中的代码,你会发现一直有一个onBind()方法我们都没有使用到,这个方法其实就是用于和Activity建立关联的,修改MyService中的代码,如下所示:
- public class MyService extends Service {
- public static final String TAG = "MyService";
- private MyBinder mBinder = new MyBinder();
- @Override
- public void onCreate() {
- super.onCreate();
- Log.d(TAG, "onCreate() executed");
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Log.d(TAG, "onStartCommand() executed");
- return super.onStartCommand(intent, flags, startId);
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Log.d(TAG, "onDestroy() executed");
- }
- @Override
- public IBinder onBind(Intent intent) {
- return mBinder;
- }
- class MyBinder extends Binder {
- public void startDownload() {
- Log.d("TAG", "startDownload() executed");
- // 执行具体的下载任务
- }
- }
- }
然后修改activity_main.xml中的代码,在布局文件中添加用于绑定Service和取消绑定Service的按钮:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/start_service"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Start Service" />
- <Button
- android:id="@+id/stop_service"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Stop Service" />
- <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>
- public class MainActivity extends Activity implements OnClickListener {
- private Button startService;
- private Button stopService;
- private Button bindService;
- private Button unbindService;
- private MyService.MyBinder myBinder;
- private ServiceConnection connection = new ServiceConnection() {
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- myBinder = (MyService.MyBinder) service;
- myBinder.startDownload();
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- startService = (Button) findViewById(R.id.start_service);
- stopService = (Button) findViewById(R.id.stop_service);
- bindService = (Button) findViewById(R.id.bind_service);
- unbindService = (Button) findViewById(R.id.unbind_service);
- startService.setOnClickListener(this);
- stopService.setOnClickListener(this);
- bindService.setOnClickListener(this);
- unbindService.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.start_service:
- Intent startIntent = new Intent(this, MyService.class);
- startService(startIntent);
- break;
- case R.id.stop_service:
- Intent stopIntent = new Intent(this, MyService.class);
- stopService(stopIntent);
- break;
- 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;
- }
- }
- }
当然,现在Activity和Service其实还没关联起来了呢,这个功能是在Bind Service按钮的点击事件里完成的。可以看到,这里我们仍然是构建出了一个Intent对象,然后调用bindService()方法将Activity和Service进行绑定。bindService()方法接收三个参数,第一个参数就是刚刚构建出的Intent对象,第二个参数是前面创建出的ServiceConnection的实例,第三个参数是一个标志位,这里传入BIND_AUTO_CREATE表示在Activity和Service建立关联后自动创建Service,这会使得MyService中的onCreate()方法得到执行,但onStartCommand()方法不会执行。
然后如何我们想解除Activity和Service之间的关联怎么办呢?调用一下unbindService()方法就可以了,这也是Unbind Service按钮的点击事件里实现的逻辑。
现在让我们重新运行一下程序吧,在MainActivity中点击一下Bind Service按钮,LogCat里的打印日志如下图所示:
另外需要注意,任何一个Service在整个应用程序范围内都是通用的,即MyService不仅可以和MainActivity建立关联,还可以和任何一个Activity建立关联,而且在建立关联时它们都可以获取到相同的MyBinder实例。
如何销毁Service
在Service的基本用法这一部分,我们介绍了销毁Service最简单的一种情况,点击Start Service按钮启动Service,再点击Stop Service按钮停止Service,这样MyService就被销毁了,可以看到打印日志如下所示:
那么如果我们是点击的Bind Service按钮呢?由于在绑定Service的时候指定的标志位是BIND_AUTO_CREATE,说明点击Bind Service按钮的时候Service也会被创建,这时应该怎么销毁Service呢?其实也很简单,点击一下Unbind Service按钮,将Activity和Service的关联解除就可以了。
先点击一下Bind Service按钮,再点击一下Unbind Service按钮,打印日志如下所示:
以上这两种销毁的方式都很好理解。那么如果我们既点击了Start Service按钮,又点击了Bind Service按钮会怎么样呢?这个时候你会发现,不管你是单独点击Stop Service按钮还是Unbind Service按钮,Service都不会被销毁,必要将两个按钮都点击一下,Service才会被销毁。也就是说,点击Stop Service按钮只会让Service停止,点击Unbind Service按钮只会让Service和Activity解除关联,一个Service必须要在既没有和任何Activity关联又处理停止状态的时候才会被销毁。
为了证实一下,我们在Stop Service和Unbind Service按钮的点击事件里面加入一行打印日志:
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.start_service:
- Intent startIntent = new Intent(this, MyService.class);
- startService(startIntent);
- break;
- case R.id.stop_service:
- Log.d("MyService", "click Stop Service button");
- Intent stopIntent = new Intent(this, MyService.class);
- stopService(stopIntent);
- break;
- case R.id.bind_service:
- Intent bindIntent = new Intent(this, MyService.class);
- bindService(bindIntent, connection, BIND_AUTO_CREATE);
- break;
- case R.id.unbind_service:
- Log.d("MyService", "click Unbind Service button");
- unbindService(connection);
- break;
- default:
- break;
- }
- }
我们应该始终记得在Service的onDestroy()方法里去清理掉那些不再使用的资源,防止在Service被销毁后还会有一些不再使用的对象仍占用着内存。