0

私は水のリマインダーアプリを構築したいです。このアプリは、事前にアプリが行ういくつかの計算に基づいて、あなたが水を飲むと思われるたびに通知を送信する必要があります。私は、ワンタイムアラーム、繰り返しアラームをスケジュールするAlarmManagerを使用する方法を知っています。私は考え出したていない何か、非常に多くのことを検索した後、私は日中、アラームの特定の数(たとえば6)毎日のスケジュールを設定するAlarmManagerを使用して、特定の時点で停止するつもり方法です(10.00pmを言います)特定の時刻(たとえば、07時)に翌日に通知を送信します。このすべては、ユーザーがアプリを開かなくても発生します。これはどうすればいいですか?ここに私のコードは次のとおりです。MainActivityのAlarmManagerスケジューリングアラーム

パート:

Long alertTime = nextAlarm.getTimeInMillis(); 
    Intent alertIntent = new Intent(this, AlertReceiver.class); 

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, 
      PendingIntent.getBroadcast(this, 1, alertIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT)); 

AlertReceiver:助けを

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.app.NotificationCompat; 

public class AlertReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent 



intent) { 
     createNotification(context, "Hydrate", "Time to drink water", "Alert"); 
    } 

public void createNotification(Context context, String msg, String msgText, String msgAlert) { 

    PendingIntent notificationIntent = PendingIntent.getActivity(context, 0, 
      new Intent(context, MainActivity.class), 0); 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.ic_drop_24dp) 
      .setContentTitle(msg) 
      .setContentText(msgText) 
      .setTicker(msgAlert); 

    notificationBuilder.setContentIntent(notificationIntent); 

    notificationBuilder.setDefaults(Notification.DEFAULT_ALL); 

    notificationBuilder.setAutoCancel(true); 

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

    notificationManager.notify(1, notificationBuilder.build()); 

    } 

} 

ありがとう!

+0

可能重複[受信機にアンドロイド送信通知ID一方セットアラーム](http://stackoverflow.com/questions/35918099/android-send-notification-id-to-receiver-while-set-alarm ) – Dario

+0

いずれかが次の日のために再スケジュール、それは発射第六時間を所望の時間のアラームを繰り返す毎日6を設定し、または、)間隔が一定であることを条件とする(毎日単一の繰り返しアラームを設定し、それを打ち消します。 –

答えて

1

あなたは、一定間隔でブロードキャストレシーバで繰り返しアラームを設定し、一定の間隔で通知を送信する現在の時刻を確認し、通知を送信する場合。

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

    //get current hour in 24 hour format 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    int curHour = calendar.get(Calendar.HOUR_OF_DAY); 

    if(curHour > 7 && curHour < 22) 
     createNotification(context, "Hydrate", "Time to drink water", "Alert"); 
}