我的理解是可以同时从多个活动绑定到同一个服务。但是,当我尝试从除 MainActivity.java 之外的第二个活动绑定到服务时,我遇到了一个反复出现的问题,从那里我用 startService 启动了服务。
在这里,我尝试从我的新活动 (SensorDataDisplay.java) 绑定到服务 (BluetoothLeService.java)。这个服务最初是在我的 MainActivity.java 活动中启动的,然后绑定在 MainActivity 中。
我已经编写了一些代码来检查绑定是否成功,并且它不断返回 false。
从第二个活动绑定时,是否需要做一些不同的事情?
SensorDataDisplay.java(第二个活动)
package com.august.customtisensortagclient;
import android.bluetooth.BluetoothGatt;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SensorDataDisplay extends AppCompatActivity {
private static final String TAG = "SensorDataDisplay";
TextView tester;
BluetoothLeServiceForLeft mBluetoothLeServiceForLeft;
boolean mBoundLeft = false;
BluetoothLeServiceForRight mBluetoothLeServiceForRight;
boolean mBoundRight;
BluetoothGatt bluetoothGattLeft;
BluetoothGatt bluetoothGattRight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor_data_display);
Intent intent = getIntent(); //From MainActivity.java
String value = intent.getStringExtra("key");//if it's a string you stored.
// Checker code (to see if successful bind)
tester = (TextView) findViewById(R.id.textView2);
tester.append(value);
}
};
}
BluetoothLeServiceForLeft.java(请原谅长度。我只是想包括这个以防有人想验证我对 LocalBinder 方法的使用。
饮歌长啸
相关分类