1

내가 알람/알림을 설정할 때마다. 알림 빌더가 즉시 트리거되고 알람 관리자와 일치하도록 '지연'을 설정하는 방법을 알지 못합니다.알림 빌더 및 알람 관리자를 동기화하는 방법?

제대로 작동하지만. 알람 관리자가 설정된 정확한 시간에 팝업됩니다.

onReceive() 경보는 모두 동일한 수신기 클래스에

public void setAlarm(Context context, int hour, int min, int notifyID) { 
    Calendar calendar = Calendar.getInstance(); 

    calendar.set(Calendar.HOUR_OF_DAY, hour); 
    calendar.set(Calendar.MINUTE, min); 
    Log.d(TAG, "setAlarm: " + hour + ":" + min); 

    if (calendar.getTimeInMillis() < System.currentTimeMillis()) { 
     calendar.add(Calendar.DAY_OF_YEAR, 1); 
     Log.d(TAG, "setAlarm: PAST SO + 1 DAY"); 
    } else { 
     Log.d(TAG, "setAlarm: FUTURE DON'T DO ANYTHING"); 
    } 

    am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

    pi = PendingIntent.getBroadcast(context, notifyID, _intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    //params alarm type, trigger time, interval, pending intent 
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * (_interval * 3600), pi); 
    Log.d(TAG, "setAlarm: " + calendar.getTimeInMillis() + _interval); 
} 

을 설정하기위한

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent intentToStartWhenAlarmSets = new Intent(context, LoginActivity.class); 
    intentToStartWhenAlarmSets.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    //I commented this part because i thought if i use the same pending intent ('pi'), they would sync. 
    //PendingIntent pendingIntent = PendingIntent.getActivity(context, 103, intentToStartWhenAlarmSets, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setContentIntent(pi) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("Content Title") 
      .setContentText("Notify " + intent.getStringExtra("medicine")) 
      .setSound(notifSound) 
      //.setVibrate(pattern) 
      //swipable 

      .setAutoCancel(true); 

    Log.d(TAG, "onReceive INTENT: " + intent.getStringExtra("medicine") + " " + intent.getStringExtra("interval")); 

    notificationManager.notify((int) System.currentTimeMillis(), builder.build()); 

방법. 여기에 매니페스트

<receiver android:name=".NotificationReceiver"/> 

전체 코드입니다.

public class NotificationReceiver extends BroadcastReceiver { 


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

    //PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
    //PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); 
    //wl.acquire(); 

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_notification); 
    remoteViews.setTextViewText(R.id.txtNotifMeds, "please work"); 
    remoteViews.setImageViewResource(R.id.imgNotif, R.mipmap.ic_launcher); 

    long[] pattern = {500, 500}; 
    //default ringtone 
    Uri notifSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    Log.d(TAG, "onReceive: its working"); 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent intentToStartWhenAlarmSets = new Intent(context, LoginActivity.class); 
    intentToStartWhenAlarmSets.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 


    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setContentIntent(pendingIntent) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("Content Title") 
      .setContentText("Notify " + intent.getStringExtra("medicine")) 
      .setSound(notifSound) 
      //.setVibrate(pattern) 
      //swipable 
      .setAutoCancel(true); 


    Log.d(TAG, "onReceive INTENT: " + intent.getStringExtra("medicine") + " " + intent.getStringExtra("interval")); 
    notificationManager.notify((int) System.currentTimeMillis(), builder.build()); 

    //wl.release(); 
    Log.d(TAG, "life cycle check: onReceive"); 
} 

//init 
private Intent _intent; 
private PendingIntent pi; 
private AlarmManager am; 
private int _interval; 
private boolean setNotif; 
private int _notifyID; 

public void setNotifData(Context context, String medicine, String interval,String uid) { 
    //initialize intent 
    _intent = new Intent(context, NotificationReceiver.class); 
    // 
    _intent.putExtra("medicine", medicine); 
    _intent.putExtra("interval", interval); 
    _intent.putExtra("uid", uid); 
    _interval = Integer.parseInt(_intent.getStringExtra("interval")); 
    context.sendBroadcast(_intent); 
    Log.d(TAG, "setNotifData: " + medicine); 
} 

public void setAlarm(Context context, int hour, int min, int notifyID) { 
    _notifyID = notifyID; 
    Log.d(TAG, "setAlarm: notify ID " + notifyID); 
    //Log.d(TAG, "setAlarm: isAlarmSet " + isAlarmSet(context)); 

    Calendar calendar = Calendar.getInstance(); 

    calendar.set(Calendar.HOUR_OF_DAY, hour); 
    calendar.set(Calendar.MINUTE, min); 
    Log.d(TAG, "setAlarm: " + hour + ":" + min); 

    if (calendar.getTimeInMillis() < System.currentTimeMillis()) { 
     calendar.add(Calendar.DAY_OF_YEAR, 1); 
     Log.d(TAG, "setAlarm: PAST SO + 1 DAY"); 
    } else { 
     Log.d(TAG, "setAlarm: FUTURE DON'T DO ANYTHING"); 
    } 

    am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

    pi = PendingIntent.getBroadcast(context, notifyID, _intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    //params alarm type, 
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * (_interval * 30), pi); 

    Log.d(TAG, "setAlarm: calendarMills" + calendar.getTimeInMillis() + " Interval: " +_interval); 
    Log.d(TAG, "life cycle check: set alarm "); 
} 

//ToDo need to add alarm != null checker in fragment. 
//ToDo still need to fix alarm setting immediately. 
public void cancelNotification(Context context, int notifyID) { 
    Intent intentCancel = new Intent(context, NotificationReceiver.class); 
    PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(context, notifyID, intentCancel, 0); 
    AlarmManager alarmManagerCancel = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

    if (alarmManagerCancel != null) { 
     alarmManagerCancel.cancel(pendingIntentCancel); 
     Log.d(TAG, "cancelNotification: notification canceled"); 
    } else { 
     Log.d(TAG, "cancelNotification: nothing to cancel"); 
    } 

} 

알림 관리자를 넣을 위치와 처음으로 실행하지 않을 위치를 파악할 수 없습니다.

답변

1

변경이 :

calendar.add(Calendar.DAY_OF_YEAR, 1); 

calendar.add(Calendar.DATE, 1); 

에이 즉시 트리거링을 중지해야합니다.

0

나는 사람들이 대답을 찾았습니다.

이것은 setNotifData() 방법입니다.

context.sendBroadcast(_intent); 

이 실수로 2 일이 걸립니다.

나는 here.에서 아이디어를 얻었다. 어리석은 실수는 나에게 2 일 걸렸다. T_T.