0

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

But when user make a choice, I want to stop the timer. But there are no onClickListener, only Intent.

How can I do that ? Thanks

Community
  • 1
  • 1
Romain Caron
  • 257
  • 1
  • 3
  • 15

1 Answers1

1

If you are using Handler like this:

    // 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);
fikr4n
  • 3,250
  • 1
  • 25
  • 46
  • Thanks, I seems to be good idea. I will try. But i'm asking, I don't think to make a timer it's good idea. But I don't find a way to keep my notification as headsup without that. – Romain Caron Feb 16 '17 at 13:55
  • @RomainCaron Actually timer by using `Handler` is intended for short time timer, i.e. as long as the application is running. For longer term, use [AlarmManager](https://developer.android.com/reference/android/app/AlarmManager.html). It will run even your app is not currently running (I mean it will run your app if it's not running). – fikr4n Feb 17 '17 at 04:21