1

사용자가 버튼을 클릭 할 때마다 특정 시간에 알림을 설정하려고합니다. 이처럼다른 시간에 여러 알림을받을 수 없습니다.

:

try { 
      Date date3 = todayTimeDateFormat.parse(todayTimeDate); 
      Date date4 = todayTimeDateFormat.parse(newTime); 

      holder.handlerGap = date4.getTime() - date3.getTime(); 
      holder.handlerGapTV.setText(String.valueOf(holder.handlerGap)); 
      holder.handlerGapTV.setVisibility(View.INVISIBLE); 
      Log.d("handlerGapTV.getText()", holder.handlerGapTV.getText().toString()); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

여기 getNotification()입니다 :

private Notification getNotification(int repeat) { 
      mBuilder = 
        new NotificationCompat.Builder(itemView.getContext()) 
          .setSmallIcon(R.mipmap.app_icon_1) 
          .setContentTitle("Notif title") 
          .setContentText("Notif text"); 

      Intent resultIntent = new Intent(itemView.getContext(), AcceptedRequest.class); 
      // Because clicking the notification opens a new ("special") activity, there's 
      // no need to create an artificial back stack. 
      PendingIntent resultPendingIntent = 
        PendingIntent.getActivity(
          itemView.getContext(), 
          repeat, 
          resultIntent, 
          PendingIntent.FLAG_UPDATE_CURRENT 
        ); 

      mBuilder.setContentIntent(resultPendingIntent); 
      return mBuilder.build(); 
     } 

을 여기 NotificationARBroadcastReceiver.java입니다 :

// make this duration fifteen minutes before startTime 
         long futureInMillis = SystemClock.elapsedRealtime() + Long.parseLong(holder.handlerGapTV.getText().toString()); 
         AlarmManager alarmManager = (AlarmManager) holder.itemView.getContext().getSystemService(Context.ALARM_SERVICE); 
         ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>(); 

         for (int i=0; i<10; ++i) { 
          Intent notificationIntent = new Intent(holder.itemView.getContext(), NotificationARBroadcastReceiver.class); 
          notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, holder.getNotification(i)); 
          PendingIntent pendingIntent = PendingIntent.getBroadcast(holder.itemView.getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
          alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); 
          intentArray.add(pendingIntent); 
         } 

Long.parseLong(holder.handlerGapTV.getText().toString())은 다음과 같이 계산 된

public class NotificationARBroadcastReceiver extends BroadcastReceiver { 

    public static String NOTIFICATION = "notification"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     Random random = new Random(); 
     int m = random.nextInt(9999 - 1000) + 1000; 

     NotificationManager mNotifyMgr = 
       (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 

     Notification notification = intent.getParcelableExtra(NOTIFICATION); 
     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     mNotifyMgr.notify(m, notification); 

    } 
} 

문제는Long.parseLong(holder.handlerGapTV.getText().toString()) 버튼이 나는 카드 2 첫 번째 버튼 및 카드 2 버튼을 누르면이 card1의 버튼 100초 경우 다음은 카드 2의 버튼 65 것을 가정하고되는 CardView 다른 점이다 그 후에 통지는 카드 2의 타이밍, 즉 100에 따라 설정되고 카드 1의 타이밍 알림은 절대로 표시되지 않습니다!

내가 원하는 것100 초 또는 65 뒤에 설정되었는지에 대한 모든 알림을 표시합니다.

도와주세요.

답변

0

음,이의 코드 변경이에

long futureInMillis = SystemClock.elapsedRealtime() + Long.parseLong(holder.handlerGapTV.getText().toString()); 
         AlarmManager alarmManager = (AlarmManager) holder.itemView.getContext().getSystemService(Context.ALARM_SERVICE); 
         ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>(); 

         for (int i=0; i<10; ++i) { 
          Intent notificationIntent = new Intent(holder.itemView.getContext(), NotificationARBroadcastReceiver.class); 
          notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, holder.getNotification(i)); 
          PendingIntent pendingIntent = PendingIntent.getBroadcast(holder.itemView.getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
          alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); 
          intentArray.add(pendingIntent); 
         } 

:

int m = (int) ((new Date().getTime()/1000L) % Integer.MAX_VALUE); 

         // make this duration fifteen minutes before startTime 
         long futureInMillis = SystemClock.elapsedRealtime() + Long.parseLong(holder.handlerGapTV.getText().toString()); 
         ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>(); 

         Intent notificationIntent = new Intent(holder.itemView.getContext(), NotificationARBroadcastReceiver.class); 
         notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, holder.getNotification()); 
         PendingIntent pendingIntent = PendingIntent.getBroadcast(holder.itemView.getContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
         AlarmManager alarmManager = (AlarmManager) holder.itemView.getContext().getSystemService(Context.ALARM_SERVICE); 
         alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); 

나를 위해 일했다!