TestServiceの全体的なリファクタリング #28

Merged
asura146 merged 73 commits from improve/refactor_service into main 2024-07-12 02:17:26 +00:00
Showing only changes of commit da545e4a5e - Show all commits

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");
}
}
}
};