diff --git a/app/src/main/java/com/example/childguard/TestService.java b/app/src/main/java/com/example/childguard/TestService.java index 300a3c1..b9df826 100644 --- a/app/src/main/java/com/example/childguard/TestService.java +++ b/app/src/main/java/com/example/childguard/TestService.java @@ -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"); + } } } };