I\'ve read through many questions and answers on Stackoverflow, and many of which just emphasize on the .cancel()
and the special unique ID. However, now matter
Not sure if this is the only problem but
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
.show();
}**strong text**
The onStart is deprecated. For a service use onStartCommand().
See the example in the developer docs:
Android Services
I use something like this in my app Audio Control and it works well.
Intent intent = new Intent(getApplicationContext(), QuickReceiver.class);
intent.putExtra("extraKeyHere", "some extra if you like");
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),
1137, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager al =
(AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
al.setRepeating(AlarmManager.RTC_WAKEUP, endCal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
This is just creating an intent that will trigger a broadcast receiver (QuickReceiver.class).
public class QuickReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
if(extras != null)
{
String endProfile = extras.getString("extraKeyHere");
Intent i = new Intent(context, QuickReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context,
1137, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager al =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
al.cancel(pi);
}
}
}
Make a note that I used "getBroadcast()" for both pendingIntents. You could start your service from the broadcast (using onStartCommand() of course :) ) and do more work there.
I suspect there is something wrong with the last(4th) argument of the method PendingIntent.getService(pickTime.this, RQS_1, intent, 0);
in your code.
Try using PendingIntent.FLAG_CANCEL_CURRENT
instead of 0, it should work.
Well for cancelling Alarm you have to create the same PendingIntent that you created while starting it. You are doing while starting,
Intent intent = new Intent(pickTime.this, timesUp.class);
PendingIntent timesUpIntent = PendingIntent
.getService(pickTime.this, RQS_1, intent, 0);
You are doing while cancelling,
Intent intent = new Intent(this, pickTime.class);
PendingIntent timesUpIntent = PendingIntent
.getBroadcast(this, RQS_1, intent, 0);
Are they same?
No, they are not same. You are creating PendingIntent
for Service
to start and trying to cancel with different PendingIntent
of BroadCast
.