大家好,我为处理程序编写了代码来处理函数并每 5 秒调用一次。我使用了下面的代码,但它显示了以下错误。这背后的主要思想是首先在 5 秒后运行它,并将值赋给 Bluetooth_service,蓝牙服务将在通知栏中显示该值。通知栏工作正常,只有值(value)。现在通过这个我想在 5 秒后更改该值。
error: incompatible types: <anonymous BleReadCallback> cannot be converted to Runnable
mHandler.postDelayed(this, 5000);
代码
Handler mHandler = new Handler();
Runnable mToastRunnable = new Runnable() {
@Override
public void run() {
if(manager.getConnectedDevices().size()<=0){
Toast.makeText(MainActivity.this,"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(MainActivity.this, "Read success! data: " + new String(data), Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this , BluetoothService.class);
intent.putExtra("inputString" ,new String(data));
startService(intent);
mHandler.postDelayed(this, 5000);
// mHandler.postDelayed(MainActivity.this.getApplication() , 5000);
TextView textView = findViewById(R.id.textView);
textView.setText(new String(data));
}
@Override
public void onFail(int failCode, String info, BleDevice device) {
// Toast.makeText(MainActivity.this, "Read fail! data: " + info, Toast.LENGTH_LONG).show();
}
});
}
}
};
private void readData(){
mToastRunnable.run();
}
蓝牙服务:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = intent.getStringExtra("inputString");
Intent notificationIntent = new Intent(this, MainActivity.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;
}
最佳答案
在类中添加新方法。例如:newMethod
并将 this
替换为 mToastRunnable
从重写方法 onRead
void newMethod(byte[] data){
Toast.makeText(MainActivity.this, "Read success! data: " + new String(data), Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this , BluetoothService.class);
intent.putExtra("inputString" ,new String(data));
startService(intent);
mHandler.postDelayed(mToastRunnable, 5000);//use mToastRunnable instead of this
// mHandler.postDelayed(MainActivity.this.getApplication() , 5000);
TextView textView = findViewById(R.id.textView);
textView.setText(new String(data));
}
主要代码
final Handler mHandler = new Handler();
final Runnable mToastRunnable = new Runnable() {
@Override
public void run() {
if(manager.getConnectedDevices().size()<=0){
Toast.makeText(MainActivity.this,"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) {
newMethod(data);//call the created method here
}
@Override
public void onFail(int failCode, String info, BleDevice device) {
// Toast.makeText(MainActivity.this, "Read fail! data: " + info, Toast.LENGTH_LONG).show();
}
});
}
}
};
private void readData(){
mToastRunnable.run();
}
关于java - 如何从 android studio 中删除不兼容类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60512925/