如何在Android中的服务中设置AlarmManager?
我有一个关于在服务中设置AlarmManager的问题。我已经创建了一个AlarmManager,它将启动一个服务,然后执行某些网络活动获取一些时间数据,然后在网络性能之后,该服务应根据从web服务获取的时间数据设置警报。现在我有一个主要的活动:如何在Android中的服务中设置AlarmManager?,android,broadcastreceiver,android-service,alarmmanager,Android,Broadcastreceiver,Android Service,Alarmmanager,我有一个关于在服务中设置AlarmManager的问题。我已经创建了一个AlarmManager,它将启动一个服务,然后执行某些网络活动获取一些时间数据,然后在网络性能之后,该服务应根据从web服务获取的时间数据设置警报。现在我有一个主要的活动: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.la
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_screen);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 21);
cal.set(Calendar.MINUTE, 44);
cal.set(Calendar.SECOND, 0);
Intent intent = new Intent(this, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0,
intent, 0);
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
}
以及启动服务的接收器:
public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Intent i = new Intent(context, PrayerTimeService.class);
context.startService(i);
}
这项服务:
public class TimeService extends Service {
@Override
public void onCreate() {
Toast.makeText(getBaseContext(), "Service is created", Toast.LENGTH_SHORT).show();
super.onCreate();
}
@Override
public void onDestroy() {
Toast.makeText(getBaseContext(), "Service is destroyed", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getBaseContext(), "Service is sunning",
Toast.LENGTH_SHORT).show();
new SetTimeTask().execute(URL_ISLAMICFINDER);
stopSelf();
return START_STICKY;
}
private class SetTimeTask extends AsyncTask<String, Void, String> {
.... some network jobs
}
现在我必须在服务中设置alarmmanager,我该怎么做
更新添加的AlarmManager:
//这些是服务中的字段,用于以字符串形式获取时间数据
私有字符串morningTimeValue;
私有字符串值;
私有字符串noonTimeValue;
私有字符串午后值;
私有字符串eveningTimeValue;
私有字符串nightTimeValue;
//转换中使用的整数值
凌晨时分;
int fajrtimin
//this is a method in the service and will be called in the onPostExecute();
public void setTimesForEvery(int hour, int min) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmForEvery.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
}
//this method converts the string time to integer values
public void convertTime(String time, int hour, int min) {
String[] array = time.split(":");
String h = array[0];
String m = array[1];
hour = Integer.parseInt(h);
min = Integer.parseInt(m);
}
// this is asynctask
private class SetTimeTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... urls) {
setPrayerTimes(urls[0]);
return true;
}
@Override
protected void onPostExecute(Boolean result) {
Toast.makeText(getBaseContext(), fajrTimeValue + "|" +
zuhurTimeValue + "|" +asrTimeValue + "|" + maghribTimeValue + "|" +
ishaTimeValue, Toast.LENGTH_LONG).show();
convertTime(fajrTimeValue, morningTimeHour, fajrTimeMin);
setTimesForEvery(21, 45);
super.onPostExecute(result);
}
}
试试这个
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), displayIntent);
我的建议是,您不应该在服务中编写报警管理器。它应该在报警广播接收器内
public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Intent i = new Intent(context, PrayerTimeService.class);
context.startService(i);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,arg1, PendingIntent.FLAG_CANCEL_CURRENT);
am.set(AlarmManager.RTC, System.currentTimeMillis() + 1000*60*60*24 ,pendingIntent);
}
如果要在服务中使用报警管理器设置。您可以在服务的onCreate方法或onStart方法中使用它
public void setTimesForEvery(String time) {
String[] array = time.split(":");
String h = array[0];
String m = array[1];
hour = Integer.parseInt(h);
min = Integer.parseInt(m);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmForEvery.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),cal.getTimeInMillis, pi);
}
你的全部要求不清楚。但是做这些事情,然后再试一次。
将super.onPostExecute行放在onPostExecute方法的顶部
并将此方法的setPrayerTimesurls[0]的值返回给onPostExecute,并将该值解析为时间参数
如果出现任何问题,请将您的完整需求以英文明确描述发送给我。此代码是否将投入使用?如果在whiche Method?中为“是”,则在没有服务的情况下,您可以使用此代码。这对我来说是成功的。但我启动服务只是为了从web获取数据。例如,我将在AsyncTask中获得09:30的时间,现在我需要设置AlarmManager在9:30通知用户,该时间可以在第二天更改,具体取决于web服务提供商等等,因此,使用这项服务的全部目的就是获得时间。因此,我的第一个AlarmManager工作正常,它启动了服务,现在我需要设置新的AlarmManager,以便使用从web获取的时间数据在该服务中安排新的时间。我如何从web获取时间数据并进行设置?哦。。。如果要获取时间和日期并将其设置为报警管理器,则实现代码以在doInBackground方法中获取时间和日期并返回结果,然后在SetTimeTask AsynckTask类的onPostExecute方法中捕获结果并设置为报警管理器。。。如果您在实施过程中遇到问题。。我可以帮你。。首先你自己试试。好的,我正在努力,如果没有,那么我会在这里发帖寻求帮助。谢谢洛蒂·德汉,我刚刚更新了我的帖子,但它不起作用,你能看看吗?setTimesForEvery21,45;这行代码只是硬编码测试,因此如果成功,将加载实时