I'm trying to schedule a service through AlarmManager
with following code:
The onCreate()
of MainActivity
is as follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
if(isMyServiceRunning(MyService.class)) {
Toast.makeText(this, "Service already Running", Toast.LENGTH_LONG).show();
}
else{
startService(new Intent(getBaseContext(), MyService.class));
}
*/
AlarmManager alarmManager;
PendingIntent alarmIntent;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int hour = 12;
int minutes = 14;
alarmManager = (AlarmManager) getSystemService(context.ALARM_SERVICE);
Intent i = new Intent (getBaseContext(), AlarmReceiver.class);
// Intent i = new Intent();
// i.setClass(getBaseContext(),MyService.class);
// alarmIntent = PendingIntent.getService(getBaseContext(), 0, i, 0);
alarmIntent = PendingIntent.getBroadcast(getBaseContext(), 0, i, 0);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
while the AlarmReceiver
is as follows:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MyService.class));
}
}
I've also tried PendingIntent.getService()
, as is visible in code.
the service was starting as required without scheduling...please suggest.
or, is it that, since setInexactRepeating
is not exact ...in that case how long will i have to wait.