在点击通知后打开应用时,如何在对话框中恢复计时器

时间:2019-06-18 03:52:04

标签: android service dialog countdowntimer

我有一个对话框,该对话框在任何推送通知到达时开始,并且持续30秒。如果应用程序位于前台,则工作正常,但如果应用程序位于后台,则主活动启动,并且必须在对话框中显示剩余的计时器。但是在这里,我可以显示对话框,但是时间又从30秒开始,这是我所不希望的。 到目前为止我尝试过的是: 1.在FirebaseMessagingService中,我设置了一个broadcastReceiver以将广播发送到其他活动以启动对话框。它工作正常,正在发送广播以开始活动。当我单击通知时,将出现对话框,但未显示计时器。 2.我试图使用服务来启动计时器,并且该服务正在启动,但计时器不起作用。 3.我在第一个广播接收器中创建了另一个广播接收器,以在对话框中设置时间。即使在后台运行也可以,但不会更新计时器值。 4. Starting countdown timer in background on receiving push notification我也使用过此方法,但是使用过此方法来更新值。

if(event.equals(eventName)){
            String remoteMessageDate = remoteMessage.getData().get(eventName).toString();
            intent = new Intent(this, DashboardMapsActivity.class);
            Gson gson = new Gson();
            booking = gson.fromJson(remoteMessageDate, BookingResult.class);
            sendAllocateBroadcast(AppConstants.INSTANCE.getRIDE_REQUEST(), booking);
            intent.putExtra(AppConstants.INSTANCE.getRIDE_REQUEST(),booking);
            Log.w(TAG, "setActions: "+ Calendar.getInstance().getTime().toLocaleString());
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
         //   intent.setFlags(Intent.FL);
            String message = "You have new booking";
            createNotification(message);
  1. BroadcastReceiver
 BroadcastReceiver rideRequestreceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.hasExtra(AppConstants.INSTANCE.getRIDE_REQUEST()))
            {
                Toast.makeText(DashboardMapsActivity.this,"booking Received",Toast.LENGTH_SHORT).show();
                booking = (BookingResult) intent.getSerializableExtra(AppConstants.INSTANCE.getRIDE_REQUEST());
                countDownTimer(booking.getBooking().getBookingId());

                setFCMNewRideDialogValue(booking);
            }
        }
    };
  1. CountDownTimer
 private void countDownTimer(final Integer bookingId) {
        Intent intent1 = new Intent(this, TimerService.class);
        startService(intent1);
        countDownTimer = new CountDownTimer(30000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
              //  Log.w(TAG, "onTick: "+ TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished));
                rideRequestDialog.setTimerValues(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished));
            }

            @Override
            public void onFinish() {
              //  Log.w(TAG, "onFinish: timer finish " );
                rideRequestDialog.rideDismiss();
                rejectRide(bookingId);
            }
        }.start();
    }
  1. OnCreate
 if (intent.hasExtra(AppConstants.INSTANCE.getRIDE_REQUEST())) {
            booking = (BookingResult) intent.getSerializableExtra(AppConstants.INSTANCE.getRIDE_REQUEST());
            if(!(rideRequestDialog.dialogShowing()))
            {
                setFCMNewRideDialogValue(booking);
            }
        } else {
            // Do something else
        }

我想显示带有工作计时器值的对话框

0 个答案:

没有答案