Notificatoin对运行

时间:2016-10-19 14:53:06

标签: java android android-intent

我最近在测试中添加了一个通知,它有一个倒数计时器,当我选择时间并点击按钮时,通知设置为计时器并正常工作。但当我在通知仍在运行时点击主页按钮,然后点击通知它会让我进入我的应用程序的新页面,而不是正在运行的那个页面。我要做的修复是在点击通知时停止计时器并让用户进入我的应用的重置页面。

//Notification Intent
    final Intent notifyIntent = new Intent(this , MainActivity.class);
    final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    final NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);



    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {



            if (view.equals(silentButton)) {

                int getvaluehour = numPickerHour.getValue();
                int getvalueminute = numPickerMin.getValue();

                getvaluehour = getvaluehour * 3600000;
                getvalueminute = getvalueminute * 60000;
                long hoursandMinstomils = getvalueminute + getvaluehour;



                final CountDownTimer counter = new CountDownTimer(hoursandMinstomils, 1000) {


                    public void onTick(long millisUntilFinished) {
                        //here you can have your logic to set text to edittext
                        long millis = millisUntilFinished;
                        //Convert milliseconds into hour,minute and seconds
                        String hms = String.format("Silent ends in: " + "%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
                        myText.setText(hms);




                        //Notification
                        notifyTimer.setSmallIcon(R.mipmap.ic_launcher);
                        notifyTimer.setTicker("This is a notification");
                        notifyTimer.setAutoCancel(true);
                        notifyTimer.setContentTitle(hms);

                        //Notification Intent
                        notifyTimer.setContentIntent(pendingIntent);

                        //issues the notification
                        nm.notify(uniqueID, notifyTimer.build());

                    }




                    public void onFinish() {

                        myText.setText("TIME'S UP!!"); //On finish change timer text

                        nm.cancel(uniqueID);



                    }
                }.start();





                stopButton.setOnClickListener(
                        new Button.OnClickListener() {
                            public void onClick(View v) {
                                counter.cancel();
                                myText.setText("Timer has been reset");

                                nm.cancel(uniqueID);

                            }
                        }
                );



            }

1 个答案:

答案 0 :(得分:0)

您可以将标志设置为意图配置其行为,为此您可以使用此

 notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);