I have the following code for a one off alarm...
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class);
Bundle bun = new Bundle();
bun.putString("data", "hello this is my message...");
intent.putExtras(bun);
PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this,
0, intent, 0);
// We want the alarm to go off 5 seconds from now. TODO
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
return false;
}
});
}
public class OneShotAlarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO
Toast.makeText(context, "Alarm! " + intent.getExtras().getString("data"), Toast.LENGTH_SHORT).show();
}
}
The alarm correctly goes off, but the "data" extra isn't getting retrieved for some reason, and is being set to null.
Thanks for the help!
Try:
Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class);
intent.putExtra("data", "hello this is my message...");
PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Possible duplicate of getExtra from Intent launched from a pendingIntent
If that doesn't help, please tell us a bit more about your files (you need to have an activity where you set up the alarm, a broadcastreceiver that starts a service, and the service where you tell the app what to do when the alarm goes off)
Citation from the doc for putExtra function:
name The name of the extra data, with package prefix.
Couldn't it be the reason?
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 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 am trying this code to popup the alarm message. Its working when launching or opening the app, but it doesn't say any popup message while outside of the app.
I am so confused, i don't know what i am doing wrong.
String alarmtime = cur.getString(cur.getColumnIndex(DBDATA.LG_ALARMTIME));
//Reminder
String[] timesplit = alarmtime.split(":");
int hour = Integer.parseInt(timesplit[0]);
int minute = Integer.parseInt(timesplit[1]);
System.out.println(hour);
System.out.println(minute);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, ShortTimeEntryReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar alarm = new GregorianCalendar();
alarm.setTimeInMillis(System.currentTimeMillis());
alarm.set(Calendar.HOUR_OF_DAY, hour);
alarm.set(Calendar.MINUTE, minute);
alarm.set(Calendar.SECOND, 0);
System.out.println(System.currentTimeMillis());
System.out.println(alarm.getTimeInMillis());
if (System.currentTimeMillis() > alarm.getTimeInMillis()){
alarm.setTimeInMillis(alarm.getTimeInMillis()+ 24*60*60*1000);// Okay, then tomorrow ...
alarmMgr.set(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(),pendingIntent);
}
else
{
alarmMgr.set(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(),pendingIntent);
}
I need to pop up the alarm message outside of the app(i.e) exactly like the alarm does.
Thanks for your help guys,
You probably need a BroadcastReceiver.
As you can read in this question : BroadcastReceiver not receiving an alarm's broadcast
You have to build the intent like this :
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, "CHECK_ALARM_CODE", alarmIntent, 0);
And receive the alarm like this :
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
Log.d("OK", "AlarmReceiver.onReceive");
}
}
Don't forget to register your broadcast in your manifest file.
How can I 'clean' values from extra intent?
Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
...
Intent intent = new Intent(this, MyBroadcastReceiver .class);
intent.putExtra("valueOne", "valOne");
intent.putExtra("valueTwo", true);
intent.putExtra("valueThree", 1);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (5 * 1000), pendingIntent);
...
}
BroadcastReceiver:
#Override
public void onReceive(Context context, Intent intent) {
...
String valueOne= intent.getStringExtra("valueOne");
Boolean valueTwo= intent.getBooleanExtra("valueTwo", false);
Integer valueThree= intent.getIntExtra("valueThree", 0);
// Log.i("info", valueOne) >> valOne
// Log.i("info", valueTwo.toString()) >> false
// Log.i("info", valueThree.toString()) >> 1
...
}
If I change value in Activity and run application again, I get same values like in first start. I try delete app from my phone/virtual machine, clean project, but problem stay :(
Anybody help me?
In the Pending Intent declaration try to set the following flag:
PendingIntent.FLAG_UPDATE_CURRENT
In your case it should be the following:
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_UPDATE_CURRENT);
First, i suggest you to format your code here. So that people here will be glad to read your problem code. Your code list above does NOT register any BroadcastReceiver to the system. You'd better to check out ApiDemo for more details.
And Also See this one
I'm creating an alarm in my app from the ItemEdit Activity. Its where one can edit/view their note/todo item, they can also set a reminder/alarm for the item there. I set the alarm with the following code:
private void createAlarm() {
Intent intent = new Intent(this, ReminderReceiver.class);
intent.putExtra("reminder_message", "Reminder Received!");
intent.putExtra("item_id", mRowId);
PendingIntent sender =
PendingIntent.getBroadcast(
getApplicationContext(),
ALARM_ID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
// Set alarm to the time given by the user.
am.set(AlarmManager.RTC_WAKEUP, mReminderCal.getTimeInMillis(), sender);
}
And here is the Receiver
public class ReminderReceiver extends BroadcastReceiver {
private static final String TAG = "MyApp";
#Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("reminder_message");
Log.v(TAG, message);
} catch(Exception e) {
Log.v(TAG, "OH SNAP!");
e.printStackTrace();
}
}
Edit: Also I have the following in my manifest:
<receiver android:process=":remote" android:name="ReminderReceiver"></receiver>
If I stay in the Activity where I set the alarm it is received fine. If I hit the back button to return to my ListActivity where all the items are listed or leave the app entirely the alarm never triggers. Have I done something wrong in setting up my alarm that it only triggers from the Activity that set it?
Thanks.
You need to look into a Service instead of an Activity for a long-living process that doesn't interact with the user (like an alarm clock).