How can I start my application on a time specified by the user.
Lets say start time is 8:00 am and end time is 4:00p.m
I am storing the value of start time and end time in shared preference.
public static synchronized void scheduleTimeoutCheck(final int time) {
final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, time / 100);
calendar.set(Calendar.MINUTE, time % 100);
calendar.set(Calendar.SECOND, 1); // Add 1 second of additional delay
if (calendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis() <= 0) {
// Time has already past, schedule for next day
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
new Timer(true).schedule(new TimerTask() {
#Override
public void run() {
// "its time for scheduleTimeoutCheck
/*if (Utility.checkSystemTimeout())
{*/
// Re - schedule this for the next day //
Utility.checkLockdownAppSchedule();
// }
}
}, calendar.getTime());
System.out.println("Timeout Check is scheduled to run at " + calendar.getTime().toString());
}
What u wanna do is fully described here
First schedule your times somewhere, like in your onCreate():
//create new calendar instance for your start time
Calendar startTime= Calendar.getInstance();
//set the time to 4AM or your USER SPECIFIED start time
startTime.set(Calendar.HOUR_OF_DAY, 4);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager) LayoutActivity.this.getSystemService(ALARM_SERVICE);
//create a pending intent to be called at 4AM
PendingIntent startPI = PendingIntent.getService(yourActivity.this, 0, new Intent("startApplicationReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);
//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
am.setRepeating(AlarmManager.RTC_WAKEUP, startTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, startPI);
//----------------------------------------------------
//create new calendar instance for your end time
Calendar endCalender = Calendar.getInstance();
//Set the time to 8PM or your USER SPECIFIED end time
endCalender.set(Calendar.HOUR_OF_DAY, 8);
endCalender.set(Calendar.MINUTE, 0);
endCalender.set(Calendar.SECOND, 0);
//create a pending intent to be called at 8 AM
PendingIntent endPI= PendingIntent.getService(yourActivity.this, 0, new Intent("endApplicationReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);
//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
am.setRepeating(AlarmManager.RTC_WAKEUP, endCalender.getTimeInMillis(), AlarmManager.INTERVAL_DAY, endPI);
Add your Alarm classes:
Start time receiver:
public class startApplicationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try { //Start your application
Intent intent = new Intent();
intent.setClassName("package.name", "<your_package_name>");
startActivity(intent);
} catch (NameNotFoundException e) { //You don't have the application installed
Log.e(TAG, e.getMessage());
}
//!!!If you WANT to repeat the start application alarm EVERY day
//Comment/Remove the next 4 lines
//Cancel start time alarm
Intent intent = new Intent(yourActivity.this, startApplicationReceiver.class);
PendingIntent startPI = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(startPI);
}
}
End time receiver
public class endApplicationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//finish your application
finish();
//!!!If you WANT to repeat the endapplication alarm EVERY day
//Comment/Remove the next 4 lines
//Cancel end time alarm
Intent intent = new Intent(yourActivity.this, endApplicationReceiver.class);
PendingIntent endPI = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(endPI);
}
}
And the last thing u need to do is register your receivers in your
Manifest.XML:
<receiver android:name="startAlarm" >
<intent-filter>
<action android:name="startApplicationReceiver" >
</action>
</intent-filter>
</receiver>
<receiver android:name="endAlarm" >
<intent-filter>
<action android:name="endApplicationReceiver" >
</action>
</intent-filter>
</receiver>
This should help u out.
Note: you must have started your application manually at least once, to register the alarm receivers.
You can use the AlarmManager for achieving such functionality.
To take a good grasp on how to implement it, you can study this good article. and this one by Java Code Geeks.
Start application it means you want to start activity at exact time?
It is not good user case i think. But for this task you have to use AlarmManager, plus a Service with your logic (Service will not visible for user).
If you want notify user about something you can use NotificationManager. Or in last case you can start semitransparent activity with dialog. Remember that for starting activity from service you should setkey SINGLE_TASK
Related
my mean goal is to run a task periodically at midnight (00:00:00)
but the user can set the period based on the interval (daily, weekly , monthly)
let's assume that this job is a backup Scheduling.
this task will triggred at midnight but based on the user preference (midnight everyday, every week , or monthly ), and if the phone was in the doze mode or even off , wait untill the next start and start backup.
when i start implementing the code , i started with JobService and JobScheduler , but unfortunately i learned that i can set the repetitive but i can't set the exact time, so the last solution was to work with alarmmanager.
i use this code for triggering the alarm :
public static void setTheTimeToStartBackup(Context context,String periode) {
int DATA_FETCHER_RC = 123;
AlarmManager mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(context, BackUpAlarmRecevier.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, DATA_FETCHER_RC,intent, PendingIntent.FLAG_UPDATE_CURRENT);
long interval = 0;
switch (periode){
case "never":
return;
case "daily":
alarmStartTime.set(Calendar.HOUR_OF_DAY, 0);
interval = AlarmManager.INTERVAL_DAY;
break;
case "weekly":
alarmStartTime.set(Calendar.DAY_OF_WEEK, 1);
interval = AlarmManager.INTERVAL_DAY*7;
break;
case "monthly":
alarmStartTime.set(Calendar.WEEK_OF_MONTH, 1);
interval = AlarmManager.INTERVAL_DAY*30;
break;
}
alarmStartTime.set(Calendar.HOUR_OF_DAY, 0);
alarmStartTime.set(Calendar.MINUTE, 0);
alarmStartTime.set(Calendar.SECOND, 0);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),interval, pendingIntent);
Log.e("Alarm","Set for midnight");
}
this is my receiver :
public class BackUpAlarmRecevier extends BroadcastReceiver {
SharedPreferences preferences;
#Override
public void onReceive(Context context, Intent intent) {
Log.e("BackUpAlarmReciver","Triggred");
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG:APP");
wl.acquire();
sendNotification(context);// simple notification...
Toast.makeText(context, "Alarm !!", Toast.LENGTH_LONG).show();
startBackupProcess();
wl.release();
}}
the problem is task never start.
so i went to test it with less time interval (15min as the minimum possible as i read ), so i change my first function setTheTimeToStartBackup to this :
public static void setTheTimeToStartBackup(Context context,String periode) {
int DATA_FETCHER_RC = 123;
AlarmManager mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 55);
Intent intent = new Intent(context, BackUpAlarmRecevier.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, DATA_FETCHER_RC,intent, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
Log.e("Alarm","Set for midnight");
}
and exactly the same problem , nothing started, no log , no notification , nothing.
and i already set the Receiver in my manifest with all permission like that :
<receiver android:name=".job.BackUpAlarmRecevier"
android:enabled="true"
android:process=":remote"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
what im doing wrong in both cases ? and if it work , this code will persist for ever or i need to set it again each time ?
thanks :)
EDIT:
i call my function setTheTimeToStartBackup in the MainActivity.
You could set it to occur at midnight if you did the appropriate time calculations. Dynamically get the current time and date, calculate when to register the broadcast for the alarm manager. Customize the onReceive method to set another alarm at 12pm again.
Either way you can trigger a broadcast receiver by registering your receiver manually.
Broadcast receiver class:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("Alarm received!! ");
// Register alarm again here.
}
}
Code to register a receiver with a custom intent filter.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager mAlarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
getApplicationContext().registerReceiver(new AlarmReceiver(), new IntentFilter("AlarmAction"));
PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, new Intent("AlarmAction"), 0);
// Add dynamic time calculations. For testing just +100 milli.
mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 100, broadcast);
;
}
You could achieve what you wanted through a background service.
My suggestion would be to turn the problem around a bit.
Create three topics on Firebase (daily, weekly, monthly). Subscribe users to appropriate topics. Have a Firebase function that is triggered by CRON job which sends the data notification down to the device, this data notification should schedule one-time WorkManager job for the update. This way you can control everything from server-side, if the phone is off, it will still execute as soon as it turns on and you don't need to manually take care of catching the Boot completed with alarm manager etc.
I've been working on this application that is supposed to run at a given time daily, except on weekends. I've used an AlarmBroadCastReceiver to fire a certain block of code at a given time. I have this code in my AlarmBroadCastReceiver class:
public void SetAlarm(Context context) {
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0, pi);
}
Basically I try and set a repeating alarm on that time.
Then at a press of a button, I have this in my MainActivity where I'm calling it from:
public void onStartAlarmClicked(View view){
Context context = this.getApplicationContext();
if(alarm != null){
Log.e(TAG, "starting alarm");
alarm.SetAlarm(context);
}else{
Log.e(TAG, "Alarm is null");
}
}
where alarm is an object of the AlarmBroadCastReceiver class.
The problem I have with it is that the code only fires once. Once it hits 2:30, it fires. However, when I set the time back to 2:29 and wait for 2:30, or set the date 1 day forward and then set the time to 2:20 and wait for 2:30, the code no longer fires.
I have a feeling I'm overlooking something rather simple with regards to setting the alarm time but I can't see it right now.
Found a better answer from this answer. Tweaked it a bit.
public void Set12nnAlarm(Context context) {
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
// every day at 9 am
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// if it's after or equal 9 am schedule for next day
if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 9) {
Log.e(TAG, "Alarm will schedule for next day!");
calendar.add(Calendar.DAY_OF_YEAR, 1); // add, not set!
}
else{
Log.e(TAG, "Alarm will schedule for today!");
}
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
}
You have to setup a BroadcastReceiver to listen to time and date changes, and then reset the alarm.
As you want the receiver to be triggered at all times, it is better to set in the Manifest.
Create class in own file
public class DateTimeChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_TIME_CHANGED) ||
action.equals(Intent.ACTION_TIMEZONE_CHANGED) ||
action.equals(Intent.ACTION_DATE_CHANGED))
{
resetAlarm();
}
}
}
And in the Manifest
<receiver android:name=".DateTimeChangeReceiver ">
<intent_filter>
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.DATE_CHANGED" />
</intent_filter>
</receiver>
Hello i want to make a background service to update the data of my app and repeat it once a day, also i want the service to start onboot. I have the following code:
public class OnBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Create Intent
context.startService(new Intent(context, BackgroundServiceHandler.class));
}
}
I have a settings menu so the user can choose the hour of the repeating alarm.
How can i reset the time of the alarmmanager? Where i have to put the code of the alarm manager? Do i have to use service or intentservice? How to check if service is running?
Alarm manager code:
Intent intent = new Intent(MainActivity.this, AlarmService.class);
intent.putExtra("i", 3);
PendingIntent pi = PendingIntent.getService(MainActivity.this, 9, intent, 0);
// every day at 9 am
Calendar calendar = Calendar.getInstance();
// if it's after or equal 9 am schedule for next day
if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 9) {
calendar.add(Calendar.DAY_OF_YEAR, 1); // add, not set!
}
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
How can i reset the time of the alarmmanager?
How to edit/reset Alarm Manager?
Where i have to put the code of the alarm manager?
For example you can place that code in static method and call it from place where user set time and from OnBootReceiver. How to implement class which will receive event OnBoot please check that answer.
Do i have to use service or intentservice?
Service vs IntentService
How to check if service is running?
Check if service is running on Android?
In my app, I want to do a certain task everyday and after that task is completed I want to trigger multiple notifications at different times (these times will be calculated while doing that task).
After googling I found out that using AlarmManager is my best option:
This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
My problems are:
1. Notification is shown only for that first time, not after that.
2. AlarmManager is triggered every time I restart that app and that past notification is shown.
PS.: I am a newbie in android and any help is appreciated
Here is what I tried:
Funtion in my activity from I am setting up this Alarm:
private void handleNotification() {
Intent alarmIntent = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
This is my AlarmReciever class:
public class AlarmReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ArrayList<String> times = new ArrayList();
GPSTracker gpsTracker = new GPSTracker(context);
//calculate the times arraylist
DateFormat format;
Date date = null;
for (String s : times) {
if (hour12) {
format = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
try {
date = format.parse(s);
} catch (ParseException e) {
}
} else {
format = new SimpleDateFormat("hh:mm", Locale.ENGLISH);
try {
date = format.parse(s);
} catch (ParseException e) {
}
}
Intent alarmIntent = new Intent(context, NotificationReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Log.d(Constants.LOG_TAG, calendar.getTime().toString());
if (Build.VERSION.SDK_INT < 19) {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
}
}
And this is my NotificationReciever class:
public class NotificationReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Constants.D) Log.d(Constants.LOG_TAG, "NOTIFICATION RECIEVER");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.sc_logo_final)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText(context.getResources().getString(R.string.app_name));
Intent resultIntent = new Intent(context, NavigationDrawerActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(NavigationDrawerActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
In my AndroidManifest.xml,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".AlarmReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".NotificationReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
First of all, this code is most likely setting up an alarm to happen at midnight of the current day, which is almost certainly in the past compared to the current time, which means you'll receive the alarm very quickly:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
According to the javadoc for setRepeating():
If the stated trigger time is in the past, the alarm will be triggered
immediately, with an alarm count depending on how far in the past the
trigger time is relative to the repeat interval.
Your Alarm does not repeat because you're using the flag INTERVAL_DAY, which only works with setInexactRepeating().
If you want to run something at midnight of the next day, you will need to add one day to the calendar. You might also consider setting the seconds field to 0 as well.
If you're registering the alarm every time the app starts, you'll keep getting that early alarm every time. Also according to the javadoc:
If there is already an alarm scheduled for the same IntentSender, it
will first be canceled.
The alarm is not repeating because you're passing an interval that is only valid for setInexactRepeating().
Hi everybody I'm trying to learn how to use AlarmManager and BroadcastReceiver in Android.
I'm having some problems with the AlarmManager:
I'm setting two alarms at 1 minute distance, but only one fires and it is some minutes late (not predictable i guess).
Here's my main activity (i'm setting Alarms by tapping a button)
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
Intent intent;
PendingIntent pendingIntent;
AtomicInteger atomicInteger;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
intent = new Intent(Constants.EXTENDED_DATA_STATUS);
atomicInteger = new AtomicInteger();
setContentView(R.layout.activity_main);
Button startButton = (Button) findViewById(R.id.start_button);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = atomicInteger.incrementAndGet();
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),id,intent,0);
Calendar firstLullo = Calendar.getInstance();
Calendar secondLullo = Calendar.getInstance();
firstLullo.set(Calendar.HOUR_OF_DAY,10);
firstLullo.set(Calendar.MINUTE,55);
secondLullo.set(Calendar.HOUR_OF_DAY,10);
secondLullo.set(Calendar.MINUTE,56);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,firstLullo.getTimeInMillis(),pendingIntent);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,secondLullo.getTimeInMillis(),pendingIntent);
}
Log.d("ALARM","settato con id " + String.valueOf(id));
}
});
}
}
My Receiver simply shows a Toast and makes the phone vibrate.
Constants.EXTENDED_DATA_STATUS is a string from a Constants class and it's added to the intent-filter of my Reveiver in the Android Manifest.
What am i missing? I googled a lot and only found that i have to use setExact() but no luck..
Thanks in advance
The reason why only one fires is because your canceling your first one always
From the docs: If there is already an alarm scheduled for the same IntentSender, that previous alarm will first be canceled.
To fix this, use different PendingIntents for both alarms.
- To let your receiver fire multiple times, reschedule it in your onReceive()
Example:
Only if u want two receivers apart from eachother!
//Declare AlarmManager
AlarmManager am = (AlarmManager) LayoutActivity.this.getSystemService(ALARM_SERVICE);
//create new calendar instance for your first alarm
Calendar startTime= Calendar.getInstance();
//set the time of your first alarm
firstLullo.set(Calendar.HOUR_OF_DAY,10);
firstLullo.set(Calendar.MINUTE, 55);
firstLullo.set(Calendar.SECOND, 0);
//create a pending intent
PendingIntent firstPI = PendingIntent.getBroadcast(yourActivity.this, 0, new Intent("yourFirstAlarmReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);
//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
am.setRepeating(AlarmManager.RTC_WAKEUP, firstAlarm.getTimeInMillis(), firstPI);
//----------------------------------------------------
//create new calendar instance for your second alarm
Calendar endCalender = Calendar.getInstance();
//Set the time alarm of your second alarm
secondLullo.set(Calendar.HOUR_OF_DAY,10);
secondLullo.set(Calendar.MINUTE,56);
secondLullo.set(Calendar.SECOND, 0);
//create a pending intent to be
PendingIntent secondPI= PendingIntent.getBroadcast(yourActivity.this, 0, new Intent("yourSecondAlarmReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);
//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
am.setRepeating(AlarmManager.RTC_WAKEUP, secondLullo.getTimeInMillis(), secondPI);
Register alarms in Manifest.XML:
<receiver android:name="firstAlarm" >
<intent-filter>
<action android:name="yourFirstAlarmReceiver" >
</action>
</intent-filter>
</receiver>
<receiver android:name="secondAlarm" >
<intent-filter>
<action android:name="yourSecondAlarmReceiver" >
</action>
</intent-filter>
</receiver>
Now u can call your alarms with:
First Alarm:
public class yourFirstAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Do something when first alarm goes off
}
}
Second Alarm:
public class yourSecondAlarmReceiverextends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Do something when second alarm goes off
}
}
Should help u out.
Yes, of course the above code will trigger only one Alarm, because you are setting same id for both alarms.
Alarm are identified and differentiated by their id in pendingintent.
Use different id for different alarms
Update Code:
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1234 ,intent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,firstLullo.getTimeInMillis(),pendingIntent);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 5678, intent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,secondLullo.getTimeInMillis(),pendingIntent);