简体   繁体   English

Android通话通知-单击时停止计时器

[英]Android call notification - Stop timer when click

I'm making a notification when a user receive a video call in my app. 当用户在我的应用中收到视频通话时,我会发出通知。

My notification : 我的通知:

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);

To repeat the notification until user answer (or limited time) I think to add a timer (like explain here : What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java? ) 要重复通知直到用户回答(或有限的时间),我想添加一个计时器(如此处的说明: Android / Java中的JavaScript setInterval / setTimeout等效于什么?

But when user make a choice, I want to stop the timer. 但是当用户做出选择时,我想停止计时器。 But there are no onClickListener, only Intent. 但是没有onClickListener,只有Intent。

How can I do that ? 我怎样才能做到这一点 ? Thanks 谢谢

If you are using Handler like this: 如果您使用这样的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;
        }
    });

Set the timer like this: 像这样设置计时器:

    handler.sendEmptyMessageDelayed(MSG_SOME_MESSAGE, SOME_DELAY_IN_MILLISECONDS);

Cancel the timer like this: 取消计时器,如下所示:

    handler.removeMessages(MSG_SOME_MESSAGE);

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

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