4

The code snippet below....

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ///////////Do something////////////////////////
    showtext.startScan();
    //SEt Alarm
    Intent intent = new Intent(this, TextReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+9000, pi);}

And my Receiver :

TextReceiver extends BroadcastReceiver{

    public void onReceive(Context context, Intent intent) {
        ///Show text/////
    }
}

The thing is that when I run the program after 9sn, I am getting an error that "The app stopped unexpectedly". Why I get this error?

My goal is to show the text every 9sn. What is the correct usage of AlarmManager in the main activity
OR Should I set alarm in the BroadcastReceiver ? Which one does makes sense: am.setRepeating or am.set in terms of my goal?

**Edit: How can I change my alarm code to run in the Broadcast Receiver ? **

Michael
  • 3,093
  • 7
  • 39
  • 83
Co Koder
  • 2,021
  • 7
  • 31
  • 39
  • Which line of code is causing the **Force Close**? You would need to analyze your logcat to find that - or post the logcat from when the app crashes. – Reed Feb 29 '12 at 06:28
  • I think this line, am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+9000, pi); – Co Koder Feb 29 '12 at 06:33
  • I don't see any reason why that line would cause a force close. Could you also post your logcat? – Reed Feb 29 '12 at 21:29
  • Unable to instantiate receiver com.co.koder.TextReceiver: java.lang.InstantiationException: com.co.koder.TextReceiver – Co Koder Mar 01 '12 at 00:30

2 Answers2

8

//try this

AlarmManager am=(AlarmManager)getApplicationContext getSystemService(Context.ALARM_SERVICE);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,  intent, PendingIntent.FLAG_CANCEL_CURRENT);

  am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),(9 * 1000), pendingIntent);
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
3

Never, ever use FLAG_CANCEL_CURRENT with PendingIntents that are set as alarms.

What happens is that you wind up canceling the validity of the PendingIntent currently held by the alarm manager, and this means that the alarm manager can no longer tell that any newly-set alarm matches that old PendingIntent. You wind up with the old (invalid) alarm still registered along with your new one. If you keep doing this you can wind up with hundreds (or more!) stale alarms registered in the system, none of which will actually do anything but which are taking up memory and CPU.

ctate
  • 1,379
  • 10
  • 11
  • Not sure if I understand this correctly - if the existing PendingIntent is being canceled and a new one is being created - why would AlarmManager not unregister the older one? – dev Aug 27 '17 at 23:08
  • 1
    The Alarm Manager doesn't know that the PendingIntent it's holding has become invalid. In particular, one thing that happens when you cancel a PendingIntent is that .equals() comparisons against it start returning false, which breaks the Alarm Manager's "search the set of existing alarms to find the one we're replacing" operation when you set() the alarm with a new PendingIntent. At alarm time the canceled one is a noop, but it's a waste of RAM until then, and a waste of battery when it rolls around to (not) dispatch. – ctate Aug 29 '17 at 00:27