Monday, 15 June 2015

alarmmanager - Unable to cancel repeating alarm Android -


I have read every question about Android, alarm manager and cancellation.

I currently start a receiver via a activity :

  tall msInterval = 1; Intent = intent (newer, this is Updater.class); Intent.setAction ("theAction"); Pending pending vertical = present = pending .gatebrastro (this, 12, intent, 0); Updater.origin = Pending elite; AlarmManager AlarmManager = (AlarmManager) getSystemService (ALARM_SERVICE); AlarmManager.Settering (Alarm Manager. RTC_wecup, System.contentimemilease (+), (MS Interval), MS Interval, pending transplantation);  

This code receiver Updator starts 1 millisecond, with the request code 12, (using 0, wrongly produces wrong behavior). It also sets the date of the updater to be fixed at the present time, which is later used to cancel the alarm.

The Updater looks like this:

  The public category updater broadcasts the receiver receiver {public static int flaggedClose = 0; Public static pending original source; Override public zero at @Receive (reference reference, intention intent) {// some work Log.w ("running", "run"); If (updater.flag close! = 0) {if (flagged close == 1) {toast. Make Text (Reference, "Finished!", Toast. LNNHTHRART). Show (); FlaggedClose ++; // Only log to toast once ("running", "off"); Origin.cancel (); AlarmManager Alarm = (AlarmManager) context.getSystemService (Event .ALARM_SERVICE); Alarms.cancel (original); }}}  

What this does now is to log the message "run", which is ~ 1000 times / s when the activity's onStop () , Updater.flaggedClose is set to 1 . I can see this in the log cat because this log alert starts printing "off" However, the alarm is still on, so every other log message is "run" and every other is "off". In the best case, the alarm goes off after a few seconds. In the worst case, I need to restart the phone. In the description of the alarm manager, it specifically says that "any alarms of any kind are closed, whose purpose is to match it (as The filter is defined by aquell (intent)) "will be canceled. Why is the alarm still being triggered?

As the CID mentioned in the comments, it is possible that "those 100" extra "alarms It has already begun "Cancel" is the only problem for someone else, there is a solution here I tested the alarm manager and it seems that you have a delay of at least 200 ms for a while. Use the handler . Examples of the question using handler:

  The public class increases the main activity activity (private bouleviation pressed = false; private boolean did = false; personal handler worker; private runnel method; tall MS Enterval = 1; @ Override Protected Zero to Create (Bundle Saved InstanceState) {Super.New (Saved InstanceState); setContentView (R.layout.activity_main); worker = New Handler (); method = getMethod (); Toggle Button = ( Tons (Searchwidioggle); Toggle.setOnClickListener {@Override Click Public Zero (see V) {if (pressed) {worker.post (method); pressed = true; } And {done = true;}}}); } @ Overhaired Protected Zero Destroy () {Super. Onesteroy (); Did = true; } Private runnable getMethod (new Runnab ()} {Public Zero Run () {log. W ("running", "run"); If (done) {toast. Make Text (getApplicationContext (), "Finish!", Toast. LNNGHHORT). Show (); Logs. W ("on", "off"); } Other {worker.postDelayed (Method, MS Interval); }}}; }}  

Click on the first button starts the handler runner, and in each call the runable calls only set the second button on the condition of press done It is thus, runenable ends after a (cleaning) round.


No comments:

Post a Comment