I have a question in regards to the setting an AlarmManager within a service. I have created an AlarmManager which will start a service which then perform certain network activity(gets some time data) and then straight after that network performance, this service should set alarm based on the time data obtained from the web services. Now I have this main activity:
@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);
}
And this receiver that starts a service:
public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Intent i = new Intent(context, PrayerTimeService.class);
context.startService(i);
}
And this service:
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
}
and now I have to set an alarmmanager here in the service how do I do that?
UPDATE added AlarmManager: //These are field in the service to get the time data as string private String morningTimeValue; private String sunriseValue; private String noonTimeValue; private String afternoonTimeValue; private String eveningTimeValue; private String nightTimeValue; //interger values used in conversion int morningTimeHour; int fajrTimeMin;
//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);
}
}