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());
}
}
ありがとう!
可能重複[受信機にアンドロイド送信通知ID一方セットアラーム](http://stackoverflow.com/questions/35918099/android-send-notification-id-to-receiver-while-set-alarm ) – Dario
いずれかが次の日のために再スケジュール、それは発射第六時間を所望の時間のアラームを繰り返す毎日6を設定し、または、)間隔が一定であることを条件とする(毎日単一の繰り返しアラームを設定し、それを打ち消します。 –