简体   繁体   English

在收到推送通知时在后台启动倒数计时器

[英]Starting countdown timer in background on receiving push notification

I want to start a countdown timer when a push notification is received. 我想在收到推送通知时启动倒数计时器。

I can receive the notification successfully. 我可以成功收到通知。

Now I want to ask, is it possible to start countdown timer in onReceive of BroadcastReceiver when a notification is received and my application is in background or foreground? 现在我想问,当收到通知并且我的应用程序处于后台或前台时,是否可以在广播接收器的onReceive中启动倒数计时器?

Simply, I should say that the timer should start whether the app is in foreground or not. 简而言之,我应该说无论应用程序是否在前台,计时器都应启动。 And when the timer is finished it should send some data to server. 计时器结束后,它应该向服务器发送一些数据。 And I need to show the remaining time in a text view as soon as app comes to foreground during the countdown. 当倒计时期间应用程序进入前台时,我需要在文本视图中显示剩余时间。

I am adding some code, please help me out. 我要添加一些代码,请帮帮我。

This is code to start the timer and this function is called in onReceive. 这是启动计时器的代码,此函数在onReceive中调用。

void startOrderCountDown() {
    CountDownTimer orderCountDown = new CountDownTimer(40000, 1000) {

        public void onTick(long millisUntilFinished) {
            Constant.remainingTimeOfOrder = millisUntilFinished / 1000;
            Log.v("Timer", "Time left: " + millisUntilFinished
                    / 1000);
        }

        public void onFinish() {
            new Thread(new Runnable() {

                @Override
                public void run() {

                    String dr_id = myPreferences.getDRIVERID();
                    String res = new ServerConnection()
                            .updateOrderMissedRejected(
                                    Constant.UPDATE_MISSED_REJECTED_ORDER,
                                    dr_id, "", "0");
                    Log.e("Timer", "finished..." + res);
                }
            }).start();
        }
    };
    orderCountDown.start();
}

But When the notification arrives, it just prints "Time left: 39" in logcat. 但是,当通知到达时,它只会在logcat中打印“剩余时间:39”。

Thanks in advance. 提前致谢。

No need for countdown timer. 无需倒数计时器。

Just follow the steps. 只需按照以下步骤操作即可。

1) Create AlarmManagerBroadcastReceiver.java 1)创建AlarmManagerBroadcastReceiver.java

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver implements IjoomerSharedPreferences {
    @Override
    public void onReceive(Context context, Intent intent) {
            // Call your sever task.
    }
}

2) set alarm from current time when you receive push notification. 2)收到推送通知时,从当前时间设置闹钟。

long INTERVAL = 40000;
AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 1010, intent, 0);

try{
    am.cancel(pi);
}catch (Exception e){
}
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + INTERVAL, pi);

2) Manifest.xml 2)Manifest.xml

<receiver  android:process=":remote" android:name="com.XXX.ZZZZ.AlarmManagerBroadcastReceiver"></receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM