同时使用两种方式启动服务

突然看到公司项目有个地方同时使用startService()和bindService()启动服务,做了个demo测试了一下,下面直接上代码:

Activity中:

package com.example.test3;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.example.service.ConnService;

public class ConnActivity extends Activity {

    private static final String tag = "ConnActivity";
    private MyServiceConnection mConn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conn);

        bindServiceMothod();
    }

    private void bindServiceMothod() {
        Intent intent = new Intent(this, ConnService.class);
        mConn = new MyServiceConnection();
        startService(intent);
        bindService(intent, mConn, Context.BIND_AUTO_CREATE);
    }

    private class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(tag, "====onServiceConnected----------------");

            ConnService.MyBinder myBinder = (ConnService.MyBinder) service;
            myBinder.sendMsg("我是Activity!");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(tag, "====onServiceDisconnected----------------");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(tag,"====onDestroy-----------------");
        if(mConn != null){
            unbindService(mConn);
        }
    }
}

Service中(记得在Manifest配置):

package com.example.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * user: uidq0530 ,date: 2017-04-05.
 * description:
 *
 * @author xhunmon
 */

public class ConnService extends Service {

    private static final String tag = "ConnService";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(tag,"======onBind---------------------------");
        return new MyBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(tag,"======onCreate---------------------------");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(tag,"======onStartCommand---------------------------");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(tag,"======onUnbind---------------------------");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(tag,"======onDestroy---------------------------");
    }

    public class MyBinder extends Binder{
        public void sendMsg(String str){
            Log.i(tag,"=====接受的数据为:"+str);
        }
    }
}

情况1,只是使用startService(),启动后退出,如下图:

情况2,只是使用bindService(),启动后退出,如下图:

情况3,同时使用用startService()和bindService(),启动后退出,如下图:

结论:当同时使用两个方法启动服务时,绑定的服务只是被解绑了,而没有被销毁!

你可能感兴趣的:(android)