Android unbindservice异常相关问题

使用serviceConnection.getService()有时会出现各种异常

换一种方式

当执行bindservice()方法时,会返回一个boolean类型的值,记录下来表示服务是否启动

在解绑时判断该值确定是否需要解绑

private boolean MQTTServiceBinded = false;
MQTTServiceBinded = mContext.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
if (null != serviceConnection) {
            if (MQTTServiceBinded) {
                mContext.unbindService(serviceConnection);
                MQTTServiceBinded = false;
            }//关闭MQTT服务
        }

 

你可能感兴趣的:(Android,service,bindservice,unbindservice)