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.