java - Android 来电通知 - 单击时停止计时器

标签 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。

我怎样才能做到这一点?谢谢

最佳答案

如果您像这样使用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);

关于java - Android 来电通知 - 单击时停止计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42270083/

相关文章:

java - 代号一/Java : Adding Jars into Project

java - 将多数组json解析为POJO

java - 如何使用Android App的ListView生成游戏日志列表?

timer - 如何从 DS3231 RTC 获得毫秒分辨率

python - 在 pygame 中暂停游戏和用户输入

java - 如何翻转 Java 原始字节中的特定位?

android - 在 Android Studio 中更改 xml 布局文件后需要重建

android - 如何获得加速度计轴和运动矢量之间的旋转?

Android 设备需要连接到服务器 - C2DM、轮询或第三方?

java - Android Java 定时器()