前提・実現したいこと
Android Studio 4.2.1、言語はJavaを使用しています。
現在Main画面ではCountDownTimerを用いて1秒ごとにカウントダウンしていくような処理を行っています。
これにAlarmManagerを用いてバックグラウンドで動作する60秒のタイマーを作成し、30秒経過したらNotificationを使用して通知を画面上部に30秒経過の通知を表示したいと思っています。
また、この通知をクリックするとアプリのメイン画面に遷移して残りのカウントダウン(30~)を表示したいと考えています。
発生している問題・エラーメッセージ
AlarmManager内でpendingIntentを作成し通知をタップするとMain画面へ飛ぶようには出来たのですが、MainActivityが再生成されてしまうためカウントが60からになってしまいます。
MainActivtyとAlarmManagerのコードは下記のとおりです。
Intent.setFlagsでフラグを変更するなど試してみましたがうまく動作しません。
MainActivityを再生成するのではなく、カウントダウン中のMain画面へ飛ぶ方法はありますでしょうか?
java
public class MainActivity extends AppCompatActivity { CountDownTimer countDownTimer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // カレンダーを30秒に設定 Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); cal.add(Calendar.SECOND, 30); long alarm_time = cal.getTimeInMillis(); //カレンダーをミリ秒に変換して変数に格納 // Pending Intent使ってレシーバーをセットする //アラームクロックインフォを作成してアラーム時間をセット AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarm_time,null); Intent intent = new Intent(MainActivity.this, AlarmManagerTest.class); PendingIntent pending = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); // アラームをセットする AlarmManager am = (AlarmManager)MainActivity.this.getSystemService(ALARM_SERVICE); if(am != null){ am.setAlarmClock(clockInfo, pending); } // 60秒のタイマーを設定 TimerStart(60); } /* CountDownTimerを1秒毎に呼び出すように設定して時間のテキストを更新していく */ public void TimerStart(long time_sec) { // 1秒毎に呼び出されるCountDownTimerを設定 countDownTimer = new CountDownTimer(time_sec*1000, 1000) { @Override public void onTick(long milliTillFinish) { // 秒を計算する int sec = (int) (milliTillFinish / 1000) % 60; // TimerTextをxx形式で更新 String timerLeftFormatted = String.format(Locale.getDefault(), "%02d", sec+1); ((TextView) findViewById(R.id.Timer_Text)).setText(timerLeftFormatted); } public void onFinish() { // 終了時の動作 countDownTimer.cancel(); ((TextView) findViewById(R.id.Timer_Text)).setText("終了"); } }; countDownTimer.start(); } }
java
public class AlarmManagerTest extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int requestCode = intent.getIntExtra("RequestCode",0); String message = "30秒経過しました"; String channelId = "default"; String title = context.getString(R.string.app_name); Intent tap_intent = new Intent(context, MainActivity.class); tap_intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, tap_intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // Notification Channel 設定 NotificationChannel channel = new NotificationChannel( channelId, title , NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription(message); channel.enableVibration(true); channel.canShowBadge(); channel.enableLights(true); channel.setLightColor(Color.BLUE); // the channel appears on the lockscreen channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); channel.setSound(defaultSoundUri, null); channel.setShowBadge(true); if(notificationManager != null){ notificationManager.createNotificationChannel(channel); Notification notification = new Notification.Builder(context, channelId) .setContentTitle(title) // android標準アイコンから .setSmallIcon(android.R.drawable.ic_lock_idle_alarm) .setContentText(message) .setAutoCancel(true) .setContentIntent(pendingIntent) .setWhen(System.currentTimeMillis()) .build(); // 通知 notificationManager.notify(R.string.app_name, notification); } } }
まだ回答がついていません
会員登録して回答してみよう