I have the below class to check if my alarms are being triggered at the exact time which I scheduled or is it differing.
I will call SetAlarm method inside the BroadCast Reciever from MainActivity. The consecutive alarms will be set by the Receiver itself by setting current time as its new string extra.
Alarms are working fine except the issue Intent string extras are not getting updated. ScheduledTime will always hold the initial value regardless of what I have set it in the setAlarm method.
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Date currentTime = new Date();
String ScheduledTime = "";
if (null != intent) { //Null Checking
ScheduledTime = intent.getStringExtra("ScheduledTime");
//intent.removeExtra("ScheduledTime");
}
String message = "Current Time" + currentTime + ", Scheduled Time was: " + ScheduledTime ;
//Show Notification
long alarmMillis =(10*60*1000) ; //Set Alarm after 10 minutes
Long newTimeInMillis = System.currentTimeMillis() + alarmMillis;
currentTime.setTime(newTimeInMillis );
setAlarm(context, newTimeInMillis , currentTime.toString());
}
public void setAlarm(Context context, Long timeMillis, String ScheduledTime)
{
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, AlarmReceiver.class);
i.putExtra("ScheduledTime", ScheduledTime);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setExact(AlarmManager.RTC_WAKEUP, timeMillis, pi);
}
}
From Android official site
public static final int FLAG_UPDATE_CURRENT
Flag indicating that if the described PendingIntent already exists,
then keep it but replace its extra data with what is in this new
Intent. For use with getActivity(Context, int, Intent, int),
getBroadcast(Context, int, Intent, int), and getService(Context, int,
Intent, int).
This can be used if you are creating intents where only the extras
change, and don't care that any entities that received your previous
PendingIntent will be able to launch it with your new extras even if
they are not explicitly given to it.
So change your code from
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
to
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
Related
I set an alarm from activity A then in activity B I can delete and update the alarm set in activity A. Updating of alarm is working but delete doesn't, I've already tried some answer from here but nothing seems to work.
I set my alarms like this.
public void Alarms() {
for(eventlist_model model:arrayList)
{
int id=model.getEvent_id();
String type=model.getEvent_type();
String date=model.getEvent_date();
String time=model.getEvent_time();
String note=model.getEvent_note();
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm");
try{
String sched=date+" "+time;
Date newSched=format.parse(sched);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(newSched);
if (calendar1.before(calendar))
calendar1.add(Calendar.DATE, 1);
Intent intent = new Intent(this, events_receiver.class).setAction("Dog_alarm");
intent.putExtra("type", type);
intent.putExtra("note", note);
intent.putExtra("id", id);
PendingIntent dog1 = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
final AlarmManager alarm1 = (AlarmManager) getSystemService(ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= 19) {
alarm1.setExact(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), dog1);
} else {
alarm1.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), dog1);
}
}catch (Exception e)
{
e.printStackTrace();
}
}
}
then in activity B I use this to cancel the alarm
public void cancelTask()
{ int id=Integer.valueOf(eventid);
Intent intent = new Intent(update_task.this, events_receiver.class);
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent task = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.cancel(task);
}
But it doesn't work and Im stuck here. Can anyone help me in this?
According to documentation of cancel method of AlarmManager:
void cancel (PendingIntent operation)
Remove any alarms with a matching Intent. Any alarm, of any type,
whose Intent matches this one (as defined by filterEquals(Intent)),
will be canceled.
It means if intentA detected to be equal withintentB, using filterEqauls Method, i.e. intentA.filterEquals(intentB)==true, then intentB in alarmanger can be used to cancel intentA which already set inside alarmManager.
filterEquals documentation notices:
boolean filterEquals (Intent other)
Determine if two intents are the same for the purposes of intent
resolution (filtering). That is, if their action, data, type, class,
and categories are the same. This does not compare any extra data
included in the intents.
It means it filterEquals does not care to extras inside intents to compare them but other paramters like Action should be exactly the same.
In your case, you've missed to call setAction in cancellation intent.
So you should change your cancelTask method like this:
public void cancelTask()
{ int id=Integer.valueOf(eventid);
Intent intent = new Intent(update_task.this, events_receiver.class);
intent.setAction("Dog_alarm");//<--- this is missed
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent task = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.cancel(task);
}
I want to get some arguments from Intent in onReceive() method of BroadcastReceiver class. But there's only int ALARM_COUNT = 1, although I put two args: my Parcelable Alarm object and test int (for the case when there is some problem with alarm object).
I set alarm like this:
private void setCurrentAlarm(Alarm alarm) {
long time = System.currentTimeMillis() + getClosestTimeDifference(alarm);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, createPendingIntent(alarm));
}
There is how I create PendingIntent variable:
private PendingIntent createPendingIntent(#NonNull Alarm alarm) {
Intent intent = new Intent(mContext, AlarmBroadcastReceiver.class);
intent.putExtra(KEY_ALARM, alarm);
intent.putExtra("TEST", 1010120012);
return PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
And onReceive() method in my AlarmBroadcaseReceiver class:
#Override
public void onReceive(Context context, Intent intent) {
Alarm receivedAlarm = intent.getParcelableExtra(KEY_ALARM); //is always null
int receivedInt = intent.getIntExtra("TEST", -1); //the same empty, -1
}
As you can see Intent contains only some ALARM_COUNT extra, but there aren't my extras.
What to do? How can I get them?
Using flags as shown in the examples below was helpful for me.
retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_CANCEL_CURRENT)
retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_IMMUTABLE)
retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_ONE_SHOT)
retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
I use AlarmManager and try to give some values in putExtra to my BroadcastReceiver. The values I send go to the BroadcastReceiver, it works fine to transmit values.
But I send my variable "counter" and I always get the old values that existed on the first start of my setRepeating(). And I know that the counter values are ways more high that I see there. So when the values change nothing happens. How can I have an event every half hour with right values?!
I've searched now for 3 hours but can't find a solution to make an interaction of my AlarmManager and some values out of a Sensor...
public void startAlarm(View view) {
try {
AlarmManager alarms = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(),
MyAlarmReceiver.class);
intent.putExtra("startStepCounter", startStepCounter);
intent.putExtra("lastStepCounter", lastStepCounter);
final PendingIntent pIntent = PendingIntent.getBroadcast(this,
1234567, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarms.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), timeToAlarmMilli, pIntent);
} catch (Exception e) {
}
}
public class MyAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("Alarm Receiver", "Entered");
//
Bundle bundle = intent.getExtras();
int local_start = bundle.getInt("startStepCounter");
int local_last = bundle.getInt("lastStepCounter");
Toast.makeText(context,
"ALARM " + local_start + " " + local_last,
Toast.LENGTH_SHORT).show();
}
}
look at this part of your code
final PendingIntent pIntent = PendingIntent.getBroadcast(this,
1234567, intent, PendingIntent.FLAG_CANCEL_CURRENT);
you need to provide uniuque id for secound part each time you use pending intent, so instead of 1234567, use a unique id.
I have read questions on stackoverflow on how to check if an alarm exists for a pending intent.
I check if my alarm exists this way:
boolean alarmUp = (PendingIntent.getBroadcast(context, 0,
new Intent("com.my.package.MY_UNIQUE_ACTION"),
PendingIntent.FLAG_NO_CREATE) != null);
I set an alarm and check is it exist and then cancel the alarm. The alarm is actually cancelled but if run the above statement again, it still returns true.
I have registered the broadcast receiver statically in manifest.
My methods to set and cancel alarms are like this:
public static void cancelRecurringAlarm(Context context, String intentAction) {
Logger.mlog(DEBUG_TAG, "cancelRecurringAlarm called with intent " + intentAction);
Intent reminder = new Intent();
reminder.setClass(context, <receiverClassName>);
reminder.setAction(intentAction);
PendingIntent recurringReminder = PendingIntent.getBroadcast(context, 0, reminder, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarms.cancel(recurringReminder);
}
public static void setRecurringAlarm(String intentAction, Context context, long timeInMs) {
Logger.mlog(DEBUG_TAG, "setRecurringAlarm called with intent " + intentAction);
Intent reminder = new Intent();
reminder.setClass(context, <receiverClassName>);
reminder.setAction(intentAction);
PendingIntent recurringReminder = PendingIntent.getBroadcast(context, 0, reminder, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.RTC, timeInMs, AlarmManager.INTERVAL_DAY, recurringReminder);
}
You are getting the Boolean value always as true because you are cancelling the alarm not the pending intent.So cancel your pending intent recurringReminder.cancel() in cancelRecurringAlarm(). Then you will get the Boolean value as false after cancelling alarm
So far and thanks to this website, I've been able to set up an alarm that will be set up and active, even if I turn of my phone.
Now, I set up a alarm to show a reminder for event A and I need the application to setup another alarm to show another reminder for event B.
I must be doing something wrong, because it only fires the reminder for event A. It seems that once set up, any other alarm is understood as the same one. :-(
Here is the detail of what I am doing in two steps:
1) From an activity I set an alarm that at certain time and date will call a receiver
Intent intent = new Intent(Activity_Reminder.this,
AlarmReceiver_SetOnService.class);
intent.putExtra("item_name", prescription
.getItemName());
intent
.putExtra(
"message",
Activity_Reminder.this
.getString(R.string.notif_text));
intent.putExtra("item_id", itemId);
intent.putExtra("activityToTrigg",
"com.companyName.appName.main.Activity_Reminder");
PendingIntent mAlarmSender;
mAlarmSender = PendingIntent.getBroadcast(
Activity_Reminder.this, 0, intent, 0);
long alarmTime = dateMgmt.getTimeForAlarm(pickedDate);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(alarmTime);
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmTime + 15000,
mAlarmSender);
2) From the receiver I call a service
Bundle bundle = intent.getExtras();
String itemName = bundle.getString("item_name");
String reminderOrAlarmMessage = bundle.getString("message");
String activityToTrigg = bundle.getString("activityToTrigg");
int itemId = Integer.parseInt(bundle.getString("item_id"));
NotificationManager nm = (NotificationManager) context.getSystemService("notification");
CharSequence text = itemName + " "+reminderOrAlarmMessage;
Notification notification = new Notification(R.drawable.icon, text,
System.currentTimeMillis());
Intent newIntent = new Intent();
newIntent.setAction(activityToTrigg);
newIntent.putExtra("item_id", itemId);
CharSequence text1= itemName + " "+reminderOrAlarmMessage;
CharSequence text2= context.getString(R.string.notif_Go_To_Details);
PendingIntent pIntent = PendingIntent.getActivity(context,0, newIntent, 0);
notification.setLatestEventInfo(context, text1, text2, pIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_ALL;
nm.notify(itemId, notification);
Thanks in Advance,
monn3t
Ok, when you set an PendingIntent, you're supposed to assign it a unique ID to it, incase you want to identify it later (for modifying/canceling it)
static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags)
//Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent).
static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)
//Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast().
The Request code is that ID.
In your code, you keep resetting the SAME PendingIntent, instead use a different RequestCode each time.
PendingIntent pIntent = PendingIntent.getActivity(context,uniqueRQCODE, newIntent, 0);
It has to be an integer, i suppose you have a primaryid (itemId) that can identify Alarm A from Alarm B.
You can set up multiple alarms by supplying different request code in pendingIntent.getBroadcast(......)
The approach which I used to setup multiple alarm is that I created a single alarm. I initialized a static integer in alarm setting class which will be incremented each time from my main activity whenever I click on "add alarm" button in my main activity.
E.g.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addAlarmClick(View v) {
AlarmActivity.broadcastCode++;
startActivity(new Intent(this, AlarmActivity.class));
}
}
AlarmActivity.java
public class AlarmActivity extends AppCompatActivity {`
//........
public static int broadcastCode=0;
//........
Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this,
broadcastCode, myIntent, 0);
For an easier way, if you are listing your Alarms by RecyclerView,
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), recyclerAdapterAlarm.getItemCount()+1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
It works for me.