Reputation: 2975
I am trying to start a service using Alarm Manager. I have tried everything but it just doesn't get started. I am calling the below mentioned code from a button listener of an activity.
private void setAlarmManager(String interval) throws NumberFormatException, IOException
{
AlarmManager service = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getApplicationContext(), MyService.class);
PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(),
0,
i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, Integer.valueOf(interval));
service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
(Integer.valueOf(interval) * 60000), pending);
}
and my manifest looks like :
<service android:enabled="true" android:name="com.pack.android.service.MyService"
/>
I am trying to start a service as per the user specified time in minutes. But my service never gets called.
If i call the service using the following code, it works. My project target version is 16.
getApplicationContext().startService(i);
Upvotes: 3
Views: 1044
Reputation: 132982
change PendingIntent.getBroadcast
to PendingIntent.getService
for starting an serivce using PendingIntent
as :
PendingIntent pending = PendingIntent.getService(getApplicationContext(),
0,
i,
PendingIntent.FLAG_CANCEL_CURRENT);
Upvotes: 4