Reputation: 21
I want to run services class for the 10:00 AM to 8:00 PM
, I tried with AlarmManager
but it's not working properly, how can I do this ?
Any one please help me.`
public void alrammanager(){
Log.d(TAG, "onStartCommand");
Toast.makeText(HomePage.this, "alarm manager start", Toast.LENGTH_LONG).show();
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyService.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 00);
// setRepeating() lets you specify a precise custom interval--in this case, // 20 minutes.
int secondsreperts = 30;
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
secondsreperts * 1000, alarmIntent);
}
Upvotes: 1
Views: 57
Reputation: 3848
Try this way to run services.
Calendar calender = Calendar.getInstance();
int hourofday = calender.get(Calendar.HOUR_OF_DAY);
if (hourofday >10 && hourofday <20){ //between 10am. and 8pm.
//start running services
}
else{
//stop running services 10am. and 8pm.
}
Upvotes: 1