1

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);
    }
}
4

3 回答 3

0
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() 方法的顶部。

并将“setPrayerTimes(urls[0])”这个方法的值返回给 onPostExecute() 并将该值解析为时间参数。

如有任何问题,请将您的完整要求收件箱,并附上清晰的英文描述。

于 2013-11-16T08:49:36.243 回答
0

我的建议是你不应该在服务中写你的警报管理器。它应该在“警报”广播接收器内

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);

}

如果你想使用服务内设置的警报管理器。您可以在 Service 的“onCreate”方法或“onStart”方法中使用它

于 2013-11-15T11:21:39.097 回答
0

尝试这个

 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
 alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), displayIntent);                
于 2013-11-15T11:14:49.850 回答