Привет всем, я сделал этот Runnable, чтобы изменить значение через 3 секунды и отправить значение в службу, которая работает в фоновом режиме. Я делаю это намерение в Runnable, поэтому он каждый раз открывает новый сервис. Значение, которое я хочу изменить каждый раз, это новая строка (данные) , но я не могу это сделать. Этот исполняемый метод находится в классе фрагментов .
Intent intent = new Intent(ModeFragment.this.getActivity() , BluetoothService.class);
intent.putExtra("inputString" ,new String(data));
ModeFragment.this.getActivity().startService(intent);
Runnable mToastRunnable = new Runnable() {
@Override
public void run() {
if(manager.getConnectedDevices().size()<=0){
Toast.makeText(ModeFragment.this.getActivity(),"No connected devices", Toast.LENGTH_LONG).show();
return;
}
BleDevice device = manager.getConnectedDevices().get(0);
Map<String , String> reciveData = getSpecificServiceInfo(device , CHARACTERISTIC_READABLE);
for (Map.Entry<String, String> e : reciveData.entrySet()){
manager.read(device, e.getKey(), e.getValue(), new BleReadCallback() {
@Override
public void onRead(byte[] data, BleDevice device) {
Toast.makeText(ModeFragment.this.getActivity(), "Read success! data: " + new String(data), Toast.LENGTH_LONG).show();
Intent intent = new Intent(ModeFragment.this.getActivity() , BluetoothService.class);
intent.putExtra("inputString" ,new String(data));
ModeFragment.this.getActivity().startService(intent);
// Intent intent1 = new Intent(ModeFragment.this.getActivity() , BluetoothActivity.class);
// intent1.putExtra("inputString" ,new String(data));
// startActivity(intent1);
}
@Override
public void onFail(int failCode, String info, BleDevice device) {
// Toast.makeText(ModeFragment.this.getActivity(), "Read fail! data: " + info, Toast.LENGTH_LONG).show();
}
});
mHandler.postDelayed(this, 3000);
}
}
};
private void readData(){
mToastRunnable.run();
}
ServiceClass:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
input = intent.getStringExtra("inputString");
Intent notificationIntent = new Intent(this, ModeFragment.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Bluetooth Services")
.setContentText(input)
.setSmallIcon(R.drawable.ic_android)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
return START_NOT_STICKY;
}