こんにちは、私のアプリケーションでは、バックグラウンド サービスとアラーム マネージャーを使用して、動的な間隔で WebView を実行しています。この時間間隔をアラーム マネージャに入力します。
ここで、私がしようとしていること
private void loadWebview() {
try {
loadPrefValues();
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Intent webViewIntent = new Intent(MyService.this, XorWebView.class);
webViewIntent.putExtra("url", mStr);
webViewIntent.putExtra("duration", mDurationStr);
webViewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pintent = PendingIntent.getActivity(MyService.this, 0,
webViewIntent, 0);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (mIntervalStr.equals("1 minute")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cur_cal.getTimeInMillis(), 1 * 60 * 1000, pintent);
}
if (mIntervalStr.equals("2 minutes")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cur_cal.getTimeInMillis(), 2 * 60 * 1000, pintent);
}
if (mIntervalStr.equals("5 minutes")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cur_cal.getTimeInMillis(), 5 * 60 * 1000, pintent);
}
if (mIntervalStr.equals("10 minutes")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cur_cal.getTimeInMillis(), 10 * 60 * 1000, pintent);
}
if (mIntervalStr.equals("30 minutes")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cur_cal.getTimeInMillis(), 30 * 60 * 1000, pintent);
}
if (mIntervalStr.equals("60 minutes")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cur_cal.getTimeInMillis(), 60 * 60 * 1000, pintent);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
System.out.println("Service.onStart()" + "Url:" + mStr
+ "Send to WebActivity");
}
このメソッドは、最初に指定された間隔でのみ実行されます。コードの何が問題なのですか?
編集#1
MainActivity では、間隔にオン/オフ スイッチ ボタンとリストビューを使用しています。サービスはこちら。
このサービス クラスでは、Alarmmanager を使用して間隔を呼び出します。
ユーザーがメインアクティビティの間隔を変更したときに、その間隔がアラームマネージャーで更新されるときに正確にしたいこと。
私のサービスクラス
public class MyService extends Service {
BroadcastReceiver mReceiver = null;
String mStr, mIntervalStr, mDurationStr;
PendingIntent pintent;
AlarmManager alarm;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(MyService.this, "Service Created", Toast.LENGTH_SHORT)
.show();
System.out.println("Service.onCreate()");
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(MyService.this, "Service started", Toast.LENGTH_SHORT)
.show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
// Let it continue running until it is stopped.
try {
loadWebview();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Toast.makeText(MyService.this, "Service command started",
Toast.LENGTH_SHORT).show();
return START_STICKY;
}
private void loadWebview() {
try {
loadPrefValues();
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Intent webViewIntent = new Intent(MyService.this, XorWebView.class);
webViewIntent.putExtra("url", mStr);
webViewIntent.putExtra("duration", mDurationStr);
webViewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pintent = PendingIntent.getActivity(MyService.this, 0,
webViewIntent, 0);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
if (mIntervalStr.equals("1 minute")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,cur_cal.getTimeInMillis(),
1 * 60 * 1000, pintent);
}
if (mIntervalStr.equals("2 minutes")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cur_cal.getTimeInMillis(), 2 * 60 * 1000, pintent);
}
if (mIntervalStr.equals("5 minutes")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cur_cal.getTimeInMillis(), 5 * 60 * 1000, pintent);
}
if (mIntervalStr.equals("10 minutes")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cur_cal.getTimeInMillis(), 10 * 60 * 1000, pintent);
}
if (mIntervalStr.equals("30 minutes")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cur_cal.getTimeInMillis(), 30 * 60 * 1000, pintent);
}
if (mIntervalStr.equals("60 minutes")) {
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cur_cal.getTimeInMillis(), 60 * 60 * 1000, pintent);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
System.out.println("Service.onStart()" + "Url:" + mStr
+ "Send to WebActivity");
}
private void loadPrefValues() {
SharedPrefManager.Init(MyService.this);
SharedPrefManager.LoadFromPref();
String s1 = SharedPrefManager.getsUrl().toString();
String s2 = SharedPrefManager.getsInterval().toString();
String s3 = SharedPrefManager.getsDuration().toString();
mStr = s1;
mIntervalStr = s2;
mDurationStr = s3;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mReceiver != null)
unregisterReceiver(mReceiver);
alarm.cancel(pintent);
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
System.out.println("Service.onDestroy()");
}
}