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

时间:2017-02-16 09:39:07

标签: java android timer

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

我的通知:

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。

我该怎么做?感谢

1 个答案:

答案 0 :(得分: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);