使用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服務
}