私は覚醒時に特定の時間間隔で通知をトリガーするアプリケーションを構築しています。alarmManagerとserviceを使用して、特定の期間にのみスケジュール通知を実行します
サービス内でアラームマネージャを実行しています。このサービスは、メインアクティビティのボタンをクリックすることで明示的に開始され、alarmManagerは特定の時間の間に通知を実行します。特定の時間帯に通知を停止するにはどうすればよいですか?ユーザーが眠っている間など、これらの通知が発せられないようにしたい。現在のユーザーが設定された間隔で通知を発射さ
私のコードは、(....これはすでに十分な長さで輸入が取り除か)以下である:ここ
public class FartSmackinChunks extends Service {
public Notification scheduleNotification;
public AlarmManager alarmScheduleManager;
public PendingIntent alarmScheduleIntent;
private Boolean autoUpdateBoolean = true;
private int intervalsGoneByInt = 0;
private Notification notification;
public static final int NOTIFICATION_ID = 1;
@Override
public void onCreate() {
// TODO: Actions to perform when service is created.
int icon = R.drawable.icon;
String tickerText = "INTERVAL FIRED";
long when = System.currentTimeMillis();
scheduleNotification = new Notification(icon, tickerText, when);
alarmScheduleManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
String ALARM_ACTION;
ALARM_ACTION = ScheduleAlarmReceiver.ACTION_REFRESH_SCHEDULE_ALARM;
Intent intentToFire = new Intent(ALARM_ACTION);
alarmScheduleIntent = PendingIntent.getBroadcast(this, 0, intentToFire,
0);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
SharedPreferences mySharedPreferences =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean autoUpdateBoolean =
mySharedPreferences.getBoolean("storedAutoUpdateBoolean", false);
String updateFreq =
mySharedPreferences.getString("storedInitialAverageTimeInterval", "00:00:00");
SimpleDateFormat dfInterval = new SimpleDateFormat("hh:mm:ss");
Date intervalTimeAsDateObject = null;
long updateFreqMilliLong;
try {
intervalTimeAsDateObject = dfInterval.parse(updateFreq);
} catch (ParseException e) {
e.printStackTrace();
}
updateFreqMilliLong = intervalTimeAsDateObject.getTime() - 18000000;
if (autoUpdateBoolean) {
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
long timetoRefresh = SystemClock.elapsedRealtime() +
updateFreqMilliLong;
alarmScheduleManager.setInexactRepeating(alarmType,
timetoRefresh, updateFreqMilliLong, alarmScheduleIntent);
notifications();
} else alarmScheduleManager.cancel(alarmScheduleIntent);
return Service.START_NOT_STICKY;
};
private void notifications() {
**notification stuff in here***
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Replace with service binding implementation.
return null;
}
@Override
public void onDestroy() {
this.alarmScheduleManager.cancel(alarmScheduleIntent);
}
}
.....と私の放送受信機の実装:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ScheduleAlarmReceiver extends BroadcastReceiver {
public static final String ACTION_REFRESH_SCHEDULE_ALARM
= "com.application.ACTION_REFRESH_SCHEDULE_ALARM";
@Override
public void onReceive(Context context, Intent intent) {
Intent startIntent = new Intent(context, SmokerReducerService.class);
context.startService(startIntent);
}
}
私はこれを実装する方法を頭に入れて少し難しいです。
alarmManagerがwaketimeで起動され、sleepTimeで停止するようにこのコードを再作成することを考えていましたが、サービス内で特定の間隔で通知メソッドを起動するTimerがありますか?これを行うより良い方法がありますか?
すべての入力をいただければ幸いです。私は今、私の頭の中でこれを数日間働かそうとしています。
おかげ
EDIT:毎日の通知のためのタイマーを使用しようとするこの出くわす @anyone:
デバイスサービスの内部で実行されるタイマーは、ランタイムによって一時停止されます(すなわち、ユーザが電話機をスタンバイ状態にする)。そのため、特定の時間間隔で通知を発行するためにタイマーを使用すると、サービス内で正しく機能しません。なぜなら、Androidがサービスを一時停止すると、間隔を捨てるタイマーも一時停止するからです。
これを行う正しい方法は、保留中のインテントの配列でAlarmManagerを使用して、その日の特定の時刻にアラームを設定することです。これにより、電話機がスタンバイ状態になっても通知(またはそのときに実行したいもの)が実行されます。
'mySharedPreferences.getBoolean'が見つかりませんでした - '// TODO:サービスが作成されたときに実行するアクション 'が必要ですか? (質問には少なくとも - 他のTODOはOKです)なぜ(長時間)サービスを使用するのですか? –
@Mr_and_Mrs_D - これを行うには、保留中のインテントの配列でAlarmManagerクラスを使用しました。 – dell116