i am trying to pass two integers through the alarmmanger but whenever i pass them the toast replies null . i can't understand where the problem is .
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
intent.putExtra("passedHour", cal.get(Calendar.HOUR_OF_DAY));
intent.putExtra("passedMin", cal.get(Calendar.MINUTE));
PendingIntent alarmIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, intent, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
alarmIntent);
This is the onreceive in alaramreceiver class
Toast.makeText(context, "Alarm!! " + intent.getStringExtra("passedHour") + ":" + intent.getStringExtra("passedMin"), Toast.LENGTH_LONG).show();
cal.get() method return int so intent is sending value as int
You need to use intent.getIntExtra method in order to get int
So in the onReceive use
Toast.makeText(context, "Alarm!! " + intent.getIntExtra("passedHour") + ":" + intent.getIntExtra("passedMin"), Toast.LENGTH_LONG).show();
Related
I'm trying to cancel the repeating notifications on a particular date, so I'm setting it in a broadcast receiver which is fired on that particular time cancelling the repeating notifications through that unique id. but it is not cancelling the notifications. I have tried two techniques both are mentioned below.
mainActivity's code where I'm setting the Alarm.
Log.v("setting range alarm","key range id = " + keyIds[j] + "time = " + RangeTimes[j]);
// Setting the alarm for repeating notifications, with different broadcast Receiver (alertIntent).
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, RangeTimes[j], AlarmManager.INTERVAL_DAY,
PendingIntent.getBroadcast(this, keyIds[j], alertIntent, PendingIntent.FLAG_UPDATE_CURRENT)); // for exact repeating THIS
Log.v("setting range alarm","key range id = " + keyIds[j] + "Cancel time = " + CancelRangeTimes[j]);
// Setting the alarm for cancellin the repeating notifications, with different broadcast Receiver(cancelIntent).
cancelIntent.putExtra("CancelID", keyIds[j]);
cancelIntent.putExtra("key", pendingIntent);
AlarmManager alarmManager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager1.set(AlarmManager.RTC_WAKEUP, CancelRangeTimes[j],
PendingIntent.getBroadcast(this, keyIds[j], cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT)); // for exact repeating THIS
here is the cancelling broadcast reciver which is not working even though it is getting called on the right time.
#Override
public void onReceive(Context context, Intent intent) {
String pleaseText = intent.getStringExtra("text");
// Methid # 1 ( By getting the Alarm id through intent and trying to cancel on the same id
/*
int cancelReqCode = intent.getIntExtra("CancelID", 0);
Log.v("setting the cancel", "inside broadCast reciver " + cancelReqCode);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, cancelReqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Log.v("setting the cancel", "inside broadCast reciver " + pendingIntent);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
*/
// Methid # 2
Log.i("okk", "please text insode cancel" + pleaseText);
PendingIntent pendingIntent = intent.getParcelableExtra("key");
Log.v("setting the cancel","inside broadCast reciver " + pendingIntent );
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
}
// below is the mainActvity's code where i'm setting alaram managers, alertIntent, CancelIntent and Pending Intents. I have omitted the code where it is getting the array values and setting them because it was useless here, but every date value is correct. this setAlarm() is getting called in the onCreate method.
public class MainActivity extends ActionBarActivity implements AsyncResponse, View.OnClickListener {
AlarmManager alarmManager;
PendingIntent pendingIntent;
public void setAlarm1() {
long[] RangeTimes = new long[C.getCount()];
long[] CancelRangeTimes = new long[C.getCount()];
String rangeCheck = "false" ;
int[] KeyRangeIds = new int[C.getCount()];
Intent alertIntent = new Intent(this, AlertReceiver.class);
Intent cancelIntent = new Intent(this, CancelAlarmBroadcastReceiver.class);
alertIntent.putExtra("strings", DealTimes);
cancelIntent.putExtra("strings", DealTimes);
// ------------------------- SETTING ALARM --------------------------------
for (int i = 0; i < UserFoodType.length; i++) {
for (int j = 0; j < C.getCount(); j++) {
RangeTimes[j] = calendar.getTimeInMillis();
CancelRangeTimes[j] = calendar1.getTimeInMillis();
}
if(FoodType[j].equals(UserFoodType[i]))
{
for (int k = 0; k < UserDealZone.length; k++) {
if(DealZone[j].equals(UserDealZone[k]))
{
Log.v("setting range alarm","key range id = " + keyIds[j] + "time = " + RangeTimes[j]);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, RangeTimes[j], AlarmManager.INTERVAL_DAY,
PendingIntent.getBroadcast(this, keyIds[j], alertIntent, PendingIntent.FLAG_UPDATE_CURRENT)); // for exact repeating THIS
Log.v("setting range alarm","key range id = " + keyIds[j] + "Cancel time = " + CancelRangeTimes[j]);
// alertIntent.putExtra("cancelID", keyIds[j]);
cancelIntent.putExtra("CancelID", keyIds[j]);
cancelIntent.putExtra("key", pendingIntent);
AlarmManager alarmManager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager1.set(AlarmManager.RTC_WAKEUP, CancelRangeTimes[j],
PendingIntent.getBroadcast(this, keyIds[j], cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT)); // for exact repeating THIS
}
}
}
You are putting an extra in cancelIntent, like this:
cancelIntent.putExtra("key", pendingIntent);
however, pendingIntent is never initialized, so it will be null.
When CancelAlarmBroadcastReceiver.onReceive() is called, this code:
PendingIntent pendingIntent = intent.getParcelableExtra("key");
is probably returning null. You are logging that, you should be able to check.
In any case, if pendingIntent is null, calling AlarmManager.cancel() won't do anything.
I'm working with alarm clock app. I want to add function that enables and disables alarm when you tap on particular area(all this should happen in listView with BaseAdapter). I use the code below and my BroadcastReceiver triggers at the right time. But!!! I can not get the data from intent extra: I get default extra.
Function to set an alarm:
public void setAlarm(int dayOfWeek, int hour, int minute, int position, int y) {
// Add this day of the week line to your existing code
Log.e("Point_1","Position " + position);
Calendar calendar = Calendar.getInstance();
Date previoudTime = calendar.getTime();
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(context, MyReceiver_Alarm.class);
intent1.putExtra("Size_ABC", y);
intent1.putExtra("key", position);
//Log.e("Point_1", "Compare1 " + calendar.getTime());
Log.e("Point_1", "Compare2 " + previoudTime);
Log.e("Point_1", "Compare " + calendar.getTime().compareTo(previoudTime));
if(calendar.getTime().compareTo(previoudTime) < 0) {
int a = calendar.get(Calendar.WEEK_OF_MONTH);
calendar.set(Calendar.WEEK_OF_MONTH,a + 1);
//Log.e("Point_1", "Less " + calendar.getTime());
}
Long alarmTime = calendar.getTimeInMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), position , intent1, 0);
//Also change the time to 24 hours.
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
Log.e("Point_1", "Time is " + calendar.getTime());
}
And this is how I get extra
Log.e("Point_1","Position_intent " + intent.getIntExtra("key",178989800));
Guess, what number I get? Yeah,right 178989800.
How to make it work right?
Thank you.
Instead of
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),
position, intent1, 0);
do
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),
position, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Adding PendingIntent.FLAG_UPDATE_CURRENT ensures that your "extras" get added to the PendingIntent.
I am trying to do something really simple! I have a sessionTimeout variable which have the value 3600000 milliseconds which means 60 minutes/1 hour. I need to set the alarm after 60 minutes. I am using the following code but calendar.getTimeInMillis gives me a very high value. If I just pass 3600000 in the alarmManager it still does not work and trigger the alarm receiver instantly.
private void setupSessionTimeoutAlarm()
{
Calendar calendar = Calendar.getInstance();
calendar.add(calendar.MILLISECOND,(int) sessionTimeout);
long timeInMilliSeconds = calendar.getTimeInMillis() + sessionTimeout;
// schedule the alarm
Intent myIntent = new Intent(Gateway.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(Gateway.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,timeInMilliSeconds,pendingIntent);
}
Calendar calendar = Calendar.getInstance();
Date date = new Date();
date.setTime(System.currentTimeMillis() + (60 * 60 * 1000));
calendar.setTime(date);
Log.i(TAG, "start: " + calendar.getTime().getMinutes());
Log.i(TAG, "start: " + calendar.getTime().getHours());
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, A3LocationUpdateReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, Configuration.LocationUpdateEveryHoursRequest, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HOUR,
pendingIntent);
I have created an app where a user can insert various subjects and their day of exam.my app provides a time span for each subjects stored in database. when time allocated for each subject reaches,an alarm should play along with a notification.notification is coming properly,but alarm sound is not playing.please help me.
My alarm manager class is :
public class AlarmManager extends BroadcastReceiver {
sampleDatabase appdb;
SQLiteDatabase sqldb;
Cursor cursor;
int today,prev;
Intent in;
#Override
public void onReceive(Context context, Intent intent)
{
NotificationManager manger = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon , "Yahrzeit" , System.currentTimeMillis());
in = new Intent(context,FirstAppActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, in, 0);
notification.setLatestEventInfo(context, "ALERT!!!!", "Time Over" , contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
appdb = new sampleDatabase(context);
sqldb = appdb.getReadableDatabase();
cursor = sqldb.query(sampleDatabase.TABLE_SEC, null, null, null, null, null, null);
cursor.moveToFirst();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, 0);
today = (int)(cal.getTimeInMillis()/1000);
Log.e("Naga","Alarm");
Log.e("today",Integer.toString(today));
prev = 0;
cursor.moveToPrevious();
while(cursor.moveToNext())
{
int dbdate = cursor.getInt(cursor.getColumnIndex(sampleDatabase.ALARMSET));
int id = cursor.getInt(cursor.getColumnIndex(sampleDatabase.ROW_ID));
Log.e("dbdate",Integer.toString(dbdate));
if((dbdate<=today)&&(dbdate>prev))
{
manger.cancelAll();
manger.notify(1, notification);
sqldb.execSQL("DELETE from " + sampleDatabase.TABLE + " where " + sampleDatabase.ROW_ID + "=" + id);
sqldb.execSQL("DELETE from "+sampleDatabase.TABLE_SEC + " where " + sampleDatabase.ROW_ID + "=" + id);
}
prev = dbdate;
}
cursor.close();
sqldb.close();
}
code which i used to call this alarm manager is like this:
alarmintent = new Intent(getApplicationContext(),com.nagainfo.firstAp.AlarmManager.class);
sender = PendingIntent.getBroadcast(getApplicationContext() , 0 , alarmintent , PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA);
am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(sender);
alarmintent = new Intent(getApplicationContext(), com.nagainfo.firstAp.AlarmManager.class);
//alarmintent.putExtra("note","Hebrew");
sender = PendingIntent.getBroadcast(getApplicationContext() , 0 , alarmintent , PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA);
am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmtime, 60000, sender);
You have not specified to use sound. Use this :
notification.defaults=Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE;
If you do not want the vibrate remove it. The point is you have to bit wise OR them. So, if you want lights also -
notification.defaults=Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE|Notification.DEFAULT_LIGHTS;
If someone are using NotificationCompat.Builder, you can use:
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notifyBuilder.setSound(sound);
I am using this code to launch an Alarm.
The alarm is set in an Activity that the user can launch.
//Setting alarm to fire off NEW_GAME intent every 24 hours.
String alarm = Context.ALARM_SERVICE;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND, 0);
Log.i("Test", "Current time: " + System.currentTimeMillis() );
Log.i("Test", "Calendar time: " + calendar.getTimeInMillis() );
int currentDate = calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE, currentDate+1);
Log.i("Test", "Calendar time with a day added: " + calendar.getTimeInMillis() );
AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm);
Intent intent = new Intent("NEW_ITEM");
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, sender);
I was told i need to supply a uniqu id so that the alarm doesnt over writte each other where getBroadcast() is.
The problem is how do I do this when the user can open the Activity as many times as they want?
Also if I supply a unique ID each time this means it could possibly set 5 of the same ALARMS because of the unique id's.
How or what is the best way to get around this?
you could always just use the unix timestamp of your target time as the unique id. that way, alarms for the exact time WILL override each other, while all other alarms will stay seperate
[EDIT:] Here is some example code:
AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm);
Intent intent = new Intent(String.valueOf(calendar.getTimeInMillis()));
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, sender);