3

I have code which is to be run daily; for this I'm trying to use AlarmManager. This is my code for triggering alarms:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = new Intent(this, AlarmReciever.class);
    PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.cancel(pi); // cancel any existing alarms
    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
        SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
        AlarmManager.INTERVAL_DAY, pi);
}

This part of the code is calling AlarmReciever class as expected, but I want the code in the AlarmReciever class to be executed only once daily. It's being called multiple times. How do I restrict it?

This is the AlarmReciever class:

public class AlarmReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        System.out.println("in alarm reciever class");

    }
} 

I'm trying to perform some business logic in the onReceive() method.

In the manifest.xml file:

<receiver android:name="com.xyz.reciever.AlarmReciever"></receiver>

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

are declared.

Nate
  • 31,017
  • 13
  • 83
  • 207
Anil M
  • 1,297
  • 2
  • 19
  • 42

1 Answers1

0

I believe the code you should be using to set your alarm would be this:

Intent i = new Intent(this, AlarmReciever.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);      // <- HERE!!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi); // cancel any existing alarms
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
    SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
    AlarmManager.INTERVAL_DAY, pi);

since your AlarmReciever is a BroadcastReceiver. Use PendingIntent.getBroadcast().

If you have much work to do in your receiver's onReceive() method, you might also delegate it to an IntentService. See this answer if you decide to do that.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • when `PendingIntent.getBroadcast()` is used, `AlarmReciever` class is not being called. – Anil M Aug 25 '13 at 05:29
  • 1
    @AnilM, you will need to make sure the receiver is listed in the AndroidManifest.xml file. For example, ``. You can [see this for a more complete tutorial on using AlarmManager this way](http://www.javacodegeeks.com/2012/09/android-alarmmanager-tutorial.html) – Nate Aug 25 '13 at 06:41
  • @AnilM, also, make sure that the spelling in the manifest xml file matches the spelling in your code. You actually spelled `AlarmReceiver` incorrectly (i <-> e) ... but the important thing is that the spellings match, of course. – Nate Aug 25 '13 at 08:28
  • @AnilM, then I think something **else** is going wrong, that you're not showing. Using `PendingIntent.getBroadcast()` is the right way to alert your `BroadcastReceiver`. Have you looked in your LogCat for any useful messages? – Nate Aug 25 '13 at 09:43