嗨,在我的应用程序中,我正在使用后台服务和警报管理器执行具有动态间隔的 webview。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 中,我使用开/关开关按钮和列表视图进行间隔。在列表视图中,我放置了 1、5、10、20、30、60 分钟。用户可以在列表视图中选择任何间隔,当开关 isChecked() 时,我开始在这里服务。
在这个服务类中,我使用 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()");
}
}