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.得到了主意愚蠢的錯誤花了我兩天。 T_T。