好的所以我已经找到了我的问题,我现在遇到一个新问题,因为我正在刷卡并更改其值,因此我的服务类中搜索栏的值不会改变。
主要活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView seekBarValue = findViewById(R.id.seekBar2);
seekbar= findViewById(R.id.seekBar1);
seekbar.setMax(20);
//我添加了意图
Intent serviceIntent = new Intent(this,BleServer.class);
serviceIntent.putExtra("seekbar",seekbar.getProgress());
this.startService(serviceIntent);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public int progressChangedValue = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressChangedValue = progress;
seekBarValue.setText(String.valueOf(progress));
Log.i(TAG, "SeekBar Value: " + progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "Seek bar progress is :" + progressChangedValue,
Toast.LENGTH_SHORT).show();
}
});
服务活动
//我添加了onStartCommand
public int onStartCommand(Intent intent, int flags, int startId) {
Integer seekvalue = intent.getIntExtra("seekbar", 0);
Log.i(TAG, "What is the Brightness Value: " + seekvalue );
//Toast.makeText(this,userID, Toast.LENGTH_LONG).show();
return START_STICKY;
}
MainActivity mainActivity = new MainActivity();
public void startServer() {
goForeground1();
Log.d(TAG, "Service: Starting Server");
mGattServer = mBluetoothManager.openGattServer(this, mGattServerCallback);
if (mGattServer == null) {
Log.w(TAG, "Unable to create GATT server");
return;
}
BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY);
BluetoothGattCharacteristic writeReadCharacteristic = new BluetoothGattCharacteristic(
desiredDimness, BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,BluetoothGattCharacteristic.PERMISSION_READ|BluetoothGattCharacteristic.PERMISSION_WRITE);
writeReadCharacteristic.setValue(write);
service.addCharacteristic(writeReadCharacteristic);
mGattServer.addService(service);
}
所以现在我有一个来自onStartCommand的值,但我无法将该值设置为onStartCommand方法之外的特性,并且该值不会随着我更改搜索条而改变。