[android-developers] Re: Help for stop service with Alarm Manager
I've seen this before and I cannot say that I know the answer for certain: it's never happened to me.
I will say this:
1) I'm deeply suspicious of the 0 you are passing as the flag used to identify the pending intent. That is the main way of identifying the intent you want to stop. Create a constant and use it.
2) The way you cancel a pending intent is by creating an *exact* copy of it. I suggest, then, that you use exactly the same code to create the intent that you use, for either starting or stopping the service. That's way less error prone.
G. Blake Meike
Marakana
Programming Android 2ed is now in stores:
http://bit.ly/programmingandroid
On Sunday, January 13, 2013 11:11:31 AM UTC-8, Antonis Kanaris wrote:
I have created the start alarm as shown below
public class MyScheduleReceiver extends BroadcastReceiver { // Restart service every 30 seconds private static final long REPEAT_TIME = 1000 * 5; @Override public void onReceive(Context context, Intent intent) { AlarmManager service = (AlarmManager) context .getSystemService(Context.ALAR
M_SERVICE ); Intent i = new Intent(context, MyStartServiceReceiver.class); PendingIntent pending = PendingIntent.getBroadcast(context , 0, i, PendingIntent.FLAG_CANCEL_CURRENT ); Calendar cal = Calendar.getInstance(); cal.add(Calendar.SECOND, 5); service.setInexactRepeating(AlarmManager .RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);I crate this for stop alarm and i call it from main activity.Manifest i think is ok...Work repeat but no stop!!!
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt
ate ); setContentView(R.layout.setup); sendBroadcast(new Intent(this,MyScheduleReceiver. class)); } public void StopRepeat(View view) { sendBroadcast(new Intent(this,MyStopReceiver.class )); } public class MyStartServiceReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Repeat service!.", Toast.LENGTH_LONG).show(); } public class MyStopReceiver extends BroadcastReceiver { // Restart service every 30 seconds private static final long REPEAT_TIME = 1000 * 5; @Override public void onReceive(Context context, Intent intent) { AlarmManager service = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE ); Intent istop = new Intent(context, MyStartServiceReceiver.class); PendingIntent pending = PendingIntent.getBroadcast(context , 0, istop, PendingIntent.FLAG_CANCEL_CURRENT ); Calendar cal = Calendar.getInstance(); cal.add(Calendar.SECOND, 5); service.cancel(pending);But the service is not stopping. What might be the issue?Thanks.
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home