午前 10 時から午後 22 時まで 3 時間ごとに通知を表示したいのですが、アプリケーションはユーザーが電話を開くたびに通知も表示します。通知プロセスがトリガーされるたびにこれを解決するために、共有設定で現在の時刻に 3 時間を加えた時間を設定しました。この時間値を使用して繰り返しアラームをトリガーしますが、正しく機能していないようです。
繰り返しアラームのスケジュールされた時間 (3 時間ごと) にのみ通知を表示し、誰かが電話を再起動するたびに通知の送信を停止するにはどうすればよいですか?
私のコードは次のとおりです。
ユーザーが電話を再起動してもトリガーされるブロードキャスト レシーバーは、次の AlarmService を呼び出しています。
public class AlarmService extends Service {
private static final String TAG = "MyService";
@Override
public void onStart(Intent intent, int startid) {
Calendar calendar ;
int sp= SharedPrefs.getDefaults("TIME", this);
Intent i = new Intent(this, NotificationBarAlarm.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, sp);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),3*60*60*1000 , pi);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service stopped", Toast.LENGTH_LONG).show();
Log.d("TAG", "onDestroy");
}
}
これは、次のブロードキャスト レシーバーにインテントを送信しています。
public class NotificationBarAlarm extends BroadcastReceiver {
NotificationManager notifyManager;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("NotificationAlarm", "onReceive");
Time time = new Time();
long currenttimeMilliseconds = System.currentTimeMillis();
time.set(currenttimeMilliseconds);
int t = time.hour;
long updatedTime= currenttimeMilliseconds + 10800000;
Time nextalarmmill = new Time();
nextalarmmill.set(updatedTime);
int nextalarm = nextalarmmill.hour;
SharedPrefs.setDefaults("TIME", nextalarm, context);
Log.d("UPDATE TIME", "The next alarm is set at"+" "+ nextalarm);
notifyManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (t >= 10 && t <= 22) {
// This Activity will be started when the user clicks the notification
// in the notification bar
Intent notificationIntent = new Intent(context,
AlarmReceiverActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
Notification notif = new Notification(R.drawable.ic_launcher,
"A new notification just popped in!",
System.currentTimeMillis());
// sound when the notification shows up.
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notif.sound = alarmSound;
notif.setLatestEventInfo(context, "Questionnaire",
"Please fill it up", contentIntent);
notifyManager.notify(1, notif);
}
}
}
Here is the code of the Shared Preferences:
public class SharedPrefs {
public static void setDefaults(String key, int value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(key, value);
editor.commit();
}
public static int getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getInt(key, 10);
}