【问题标题】:Android call notification - Stop timer when clickAndroid 呼叫通知 - 单击时停止计时器
【发布时间】:2017-07-05 08:03:51
【问题描述】:

当用户在我的应用中收到视频通话时,我会发出通知。

我的通知:

Intent intent1 = new Intent(Intent.ACTION_CAMERA_BUTTON);
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent1,0);
        NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_camera_notification,"Ok",pendingIntent);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle("TITRE !!")
                .setContentText("Test")
                .setPriority(Notification.PRIORITY_HIGH)
                .setCategory(Notification.CATEGORY_CALL)
                .setSmallIcon(R.drawable.ic_camera_notification)
                .addAction(action)
                .setSound(uri)
                .setAutoCancel(true)
                .setVibrate(new long[] { 1000, 1000});
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        Notification notification = builder.build();
        notificationManager.notify(11,notification);

要重复通知直到用户回答(或有限时间),我想添加一个计时器(如在此处解释:What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?

但是当用户做出选择时,我想停止计时器。但是没有onClickListener,只有Intent。

我该怎么做?谢谢

【问题讨论】:

    标签: java android timer


    【解决方案1】:

    如果你像这样使用Handler

        // Message identifier
        private static final int MSG_SOME_MESSAGE = 1234;
    
        private Handler handler;
    
        ...
    
        handler = new Handler(new Handler.Callback() {
    
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == MSG_SOME_MESSAGE) {
                    // do something
                    // to repeat, just call sendEmptyMessageDelayed again
                    return true;
                }
                return false;
            }
        });
    

    这样设置计时器:

        handler.sendEmptyMessageDelayed(MSG_SOME_MESSAGE, SOME_DELAY_IN_MILLISECONDS);
    

    像这样取消计时器:

        handler.removeMessages(MSG_SOME_MESSAGE);
    

    【讨论】:

    • 谢谢,我似乎是个好主意。我会尝试。但我在问,我不认为做一个计时器是个好主意。但如果没有它,我找不到办法让我的通知保持提示。
    • @RomainCaron 实际上使用Handler 的计时器是为短时间计时器设计的,即只要应用程序正在运行。如需长期使用,请使用AlarmManager。即使您的应用当前未运行,它也会运行(我的意思是,如果它未运行,它将运行您的应用)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多