From da545e4a5eff3434b9a0cd46c0fe2315968a5a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=8D=E3=82=80=E3=81=AD=E3=81=93?= Date: Thu, 11 Jul 2024 15:47:02 +0900 Subject: [PATCH] =?UTF-8?q?5=E5=88=86=E4=BB=A5=E5=86=85=E3=81=AB=E5=86=8D?= =?UTF-8?q?=E6=8E=A5=E7=B6=9A=E3=81=95=E3=82=8C=E3=81=9F=E3=81=A8=E3=81=8D?= =?UTF-8?q?=EF=BC=8C=E9=80=9A=E7=9F=A5=E3=82=92=E3=82=AD=E3=83=A3=E3=83=B3?= =?UTF-8?q?=E3=82=BB=E3=83=AB=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/TestService.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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"); + } } } };