1

私はアラーム/通知を設定する度に。通知ビルダはすぐにトリガし、アラームマネージャに一致するように「遅延」を設定する方法はわかりません。通知ビルダとアラームマネージャを同期する方法は?

「正しく」動作していますが。アラームマネージャが設定されたときに、適切なタイミングでポップします。

onReceive()

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()); 

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); 
} 

両方が同じ受信機のクラスにあるアラームを設定する方法。ここでは、マニフェスト

<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.DATE, 1); 

calendar.add(Calendar.DAY_OF_YEAR, 1); 

これはすぐにトリガーからそれを停止する必要があります。

0

私は答えを見つけました。

これはsetNotifData()の方法です。

context.sendBroadcast(_intent); 

この間違いは-.-私に2日を要しました。

私はhere.からアイデアを得ました。愚かな間違いは私に2日間かかりました。 T_T。

関連する問題