Android Service Binder连接池(一)

该代码实现了一个TestServiceManager类,用于管理与AndroidService的连接。它使用ServiceConnection进行服务绑定,当服务连接成功时,通过回调通知客户端。同时,它包含了对Binder死亡的处理,当Binder死亡时会重新绑定服务。此外,类采用了单例模式确保唯一实例。
摘要由CSDN通过智能技术生成

开发中一般不需要第三方直接编写连接我们服务的代码,我们需要提供一个Service的管理类

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.util.Log;


public class TestServiceManager {
    private static final String TAG = "TestServiceManager";
    private TestCallBack mTestCallBack;
    private Context mContext;
    private ITestService testBinder;
    private volatile static TestServiceManager instance;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG,"===onServiceConnected==="+(service != null));
            if (service!=null){
               testBinder = ITestService.Stub.asInterface(service);
                if (mTestCallBack!=null){
                    mTestCallBack.success(testBinder);
                }
                try {
                    service.linkToDeath(deathRecipient,0);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG,"===onServiceDisconnected===");
            if (mTestCallBack!=null){
                mTestCallBack.fail();
            }
            testBinder = null;
        }
    };
    //死亡代理,断开重连
    private IBinder.DeathRecipient deathRecipient=new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            Log.i(TAG,"===binderDied===");
            if(testBinder!=null){
                testBinder.asBinder().unlinkToDeath(deathRecipient,0);
                testBinder=null;
                bind();
            }
        }
    };
    private TestServiceManager() {

    }
   //实现单例
    public static TestServiceManager getInstance() {
        if (instance == null) {
            synchronized (TestServiceManager.class) {
                if (instance == null) {
                    instance = new TestServiceManager();
                }
            }
        }
        return instance;
    }

    public void init(Context context, TestCallBack testCallBack) {
        Log.i(TAG,"===init===");
        mTestCallBack = testCallBack;
        mContext = context;
        bind();
    }
    //连接服务
    private void bind(){
        Log.i(TAG,"===bind===");
        if (mContext!=null){
            Intent intent = new Intent("com.test");
            intent.setPackage("com.test");
            mContext.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        }
    }
    public void release(){
        Log.i(TAG,"===release===");
        mContext.unbindService(serviceConnection);
        testBinder = null;
        instance = null;
    }

    public ITestService getTestBinder() {
        return testBinder;
    }

    public interface TestCallBack {
        void success(ITestService service);
        void fail();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值