2017-02-16 36 views
0

我在用户在我的应用程序中接收视频通话时发出通知。Android通话通知 - 点击时停止计时器

我的通知:

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

如果您正在使用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); 
+0

谢谢,我似乎是个好主意。我会尝试。 但我问,我不认为做一个计时器是个好主意。但我没有找到一种方法来保持我的通知没有这样的头像。 –

+0

@RomainCaron实际上,使用'Handler'的计时器只适用于短时间计时器,即只要应用程序正在运行。长期来说,使用[AlarmManager](https://developer.android.com/reference/android/app/AlarmManager.html)。它会运行,即使你的应用程序目前没有运行(我的意思是它会运行你的应用程序,如果它没有运行)。 – BornToCode