5分以内に再接続されたとき,通知をキャンセルするように

This commit is contained in:
ろむねこ 2024-07-11 15:47:02 +09:00
parent 286c178f7f
commit da545e4a5e
No known key found for this signature in database
GPG Key ID: FA1F39A1BA37D168

View File

@ -28,6 +28,9 @@ import com.google.firebase.firestore.FirebaseFirestore;
public class TestService extends Service {
private Handler handler = new Handler();
private Runnable notificationRunnable;
public static class NotificationContent {
private final String title;
private final String description;
@ -266,16 +269,24 @@ public class TestService extends Service {
SharedPreferences.Editor e = pref.edit();
String action = intent.getAction(); // may need to chain this to a recognizing function
boolean isInCar = pref.getBoolean("isInCarPref", false);
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action) && !isInCar) {//bluetoothが切断されたときに乗車状態のとき
// 5分待機する
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action) && !isInCar) {
// bluetoothが切断されたときに乗車状態のとき
notificationRunnable = new Runnable() {
@Override
public void run() {
// 5分経過した時点でも車に乗っていない場合
Notification(context, BLUETOOTH_NOTIFICATION);
}
};
}, 5 * 60 * 1000); // 5分をミリ秒に変換
handler.postDelayed(notificationRunnable, 5 * 60 * 1000); // 5分をミリ秒に変換
} else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// 再接続された場合通知をキャンセルする
if (notificationRunnable != null) {
handler.removeCallbacks(notificationRunnable);
notificationRunnable = null;
Log.d("BT", "Notification canceled due to reconnection");
}
}
}
};