Hello friends that I developed an application Creating courses. I want to give notice when the time course.
So if you have classes Monday at 8 o'clock. 8 hours per week of class time came. I want to create a notification. I'm doing this with the alarmmanager and notification. I can give you one-time alarms. But somehow I could not repeat. Codes as follows. I will not try it because it is part of recevice.
private void alarmla() {
SQLiteDatabase db=activity.openOrCreateDatabase("SINIFD",activity.MODE_PRIVATE,null);
Cursor c = db.rawQuery("select * from ders", null);
if(c.getCount()>0){
c.moveToFirst();
do {
AlarmManager alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
Toast.makeText(activity, c.getString(3).split(":")[0] + " " + c.getString(3).split(":")[1], Toast.LENGTH_LONG).show();
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
notificationIntent.putExtra("message", c.getString(1) + " dersi zamanı geldi. Kazanımlara ulaşmak için tıklayınız. İyi dersler.");
notificationIntent.putExtra("gizli", c.getString(6));
PendingIntent broadcast = PendingIntent.getBroadcast(activity, c.getInt(0), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar timeOff = Calendar.getInstance();
int days = Calendar.FRIDAY + (7 - timeOff.get(Calendar.DAY_OF_WEEK)); // how many days until Sunday
timeOff.add(Calendar.DATE, days);
timeOff.set(Calendar.HOUR, Integer.parseInt(c.getString(3).split(":")[0])); //buraya 22 geliyor databaseden
timeOff.set(Calendar.MINUTE,Integer.parseInt(c.getString(3).split(":")[1])); //buraya 10 geliyor mesala
timeOff.set(Calendar.SECOND, 0);
timeOff.set(Calendar.MILLISECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeOff.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, broadcast);
} while (c.moveToNext());
c.close();
}
db.close();
Toast.makeText(activity,"Vay be ayar ",Toast.LENGTH_LONG).show();
}
When there is no problem with the database of the adjustment.
Do not forget relaunch alarm manager after rebooting your mobile device!
Use broadcast receiver to catch this action
More about reboot : Trying to start a service on boot on Android
Related
Hello Below is my code to Start the alarm in my application.
public static void startReferAlarm(Context context,String[] message,String activityToOpen)
{
try {
Log.d("Refer friend Activity", "Alarm On");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
Intent myIntent = new Intent(context, AlarmReciever.class);
myIntent.putExtra("message",message);
myIntent.putExtra("äctivityName",activityToOpen);
final int _id = (int) System.currentTimeMillis();
PendingIntent appIntent = PendingIntent.getBroadcast(context, _id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//48*60*60*1000
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
48*60*60*1000, appIntent);
}catch (Exception e) {
Log.d("MyActivity", "Alarm Off");
e.printStackTrace();
}
}
From my understating above code will start the alarm and will notify on 8 am. and then will repeat after 48 hours.
But I want to start the alarm after 48 hours or lets say after 7 hours and then it repeat after every 48 hours.
Please help me with the logic.Thanks in advance.
Do not use calender.set(Calendar.HOUR, 8) as it will set hours to 8'o clock
Try
//if you want to add 7 hours
calender.add(Calender.HOUR, 7); //this will add 7 hours to current time
//if you want to add 7 days
calender.add(Calender.DAY_OF_MONTH, 7); //this will add 7 days to current time
// interval to repeat alarm after 48hours
int interval = 48 * 60 * 60 * 1000;
Replace Calendar.HOUR with Calendar.HOUR_OF_THE_DAY if you want to use it in 24hours format.
See the answer for repeating alarm issue How to repeat alarm in android 6.0
For any one who is still confuse. Below method will initiate the alarm after 7 days of any particular action it will notify on 9 am and will repeat after 48 hours.
public static void startCreateProfileAlarm(Context context,AlarmManager alarmManager ,String[] message,String activityToOpen) {
try {
//working code
Calendar calendar = Calendar.getInstance();
calendar.add(DAY_OF_MONTH,7);
//calendar.add(Calendar.HOUR_OF_DAY,1);
calendar.set(Calendar.HOUR_OF_DAY, 9);
// we can set any time here
//calendar.set(Calendar.HOUR_OF_DAY, 10);
Intent myIntent = new Intent(context, AlarmReciever.class);
myIntent.putExtra("message",message);
myIntent.putExtra("äctivityName",activityToOpen);
final int _id = (int) System.currentTimeMillis();
createProfileAppIntent = PendingIntent.getBroadcast(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//48*60*60*1000
//2*60*1000
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
48*60*60*1000, createProfileAppIntent);
}catch (Exception e) {
Log.d("MyActivity", "Alarm Off");
e.printStackTrace();
}
}
I have a working alarm using the alarmManager class. If I set the alarm for any time before midnight of the current day everything is fine. If I want to set the alarm for the 7 a.m., however, and 7 a.m. has already come and gone for today, that of course does not work.
Is there a way to do this without implementing datepicker and dates into the program?
Below is a code sample. I can post more complete code if needed.
Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, myIntent, 0);
repeatInterval = LoadPreferences("repeatInterval", repeatInterval); //gets number of minutes reminder should repeat
repeatIntervalMilliseconds = repeatInterval * 1000 * 60; //converts repeating interval to milliseconds for setRepeating method
//Set a one time alarm
if (repeatInterval == 0) {
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
AlarmReceiver alarmReceiver = new AlarmReceiver(this); //http://stackoverflow.com/questions/16678763/the-method-getapplicationcontext-is-undefined
Toast.makeText(AlarmActivity.this, "Your one time reminder is now set for " + hourSet + ":" + minuteSetString + amPmlabel, Toast
.LENGTH_LONG)
.show();
}
//Set a repeating alarm
else {
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), repeatIntervalMilliseconds, pendingIntent);
AlarmReceiver alarmReceiver = new AlarmReceiver(this); //http://stackoverflow.com/questions/16678763/the-method-getapplicationcontext-is-undefined
Toast.makeText(AlarmActivity.this, "Your reminder is now set for " + hourSet + ":" + minuteSetString + amPmlabel + " and will " +
"repeat " +
"every " +
repeatInterval + " minutes.", Toast.LENGTH_LONG).show();
}
I am testing this solution as recommended by Mike M. I will know tomorrow morning if this works. Mike if you want to post the same solution separately, and this is successful (and I think it will be), I can award you points.
//Add 1 day to alarmTime if alarmTime has already gone by for today
calendarComparison = now.compareTo(alarmTime);
if (calendarComparison == 1){
alarmTime.add(Calendar.DATE, 1);
}
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);
I am using this code to set a Alarm everyday for 8 oclock the next day.
I am setting this alarm in an activity that can be opened based upon the user.
//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);
My only question is..Lets say at 10:00 o clock today am. i open the activity that alarm is set for tomorrow..Lets say i open the activity again at 12:00 am mid-night, will the alarm set earlier that day be overr written by the current alarm being set?
If you use the same request number (second parameter) while creating the PendingIntent object
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
then it will overwrite the current PendingIntent and hence will replace the current Alarm.
It will also depend on what you pass as the last parameter to it. Possible values given in the constants section here.
I'm trying to make Android fire a Alarm at a certain time the user specifies to check if the user has posted to the service. However, Android won't fire the intent.
AndroidManifest.xml:
<receiver
android:name="me.kennydude.dailybooth.NoBoothNotify.AlarmReciever" />
<service
android:name="me.kennydude.dailybooth.NoBoothNotify.AlarmService">
</service>
NoBoothNotify.java function that sets the alarm:
public static void settingsChanged(){
Context cntxt = DailyboothApplication.getInstance();
String value = DailyboothShared.getPersonalSetting("noBoothNotify", "no");
AlarmManager alm = (AlarmManager) cntxt.getSystemService(Context.ALARM_SERVICE);
Intent piI = new Intent(cntxt, AlarmReciever.class);
if(Build.VERSION.SDK_INT >= 5){
SharedPreferences prefs = DailyboothShared.getPrefs();
piI.putExtra("account", prefs.getString("current_account", null));
}
PendingIntent pi = PendingIntent.getBroadcast(cntxt, 348347873, piI, PendingIntent.FLAG_UPDATE_CURRENT);
if(value.equals("no")){
alm.cancel(pi);
} else{
String[] values = value.split(":");
Calendar cal = Calendar.getInstance();
Log.d("s", value);
// cal.setTimeInMillis(System.currentTimeMillis());
cal.clear(Calendar.HOUR_OF_DAY);
cal.clear(Calendar.SECOND);
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(values[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(values[1]));
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
Log.d("s", "setting for " + cal.getTimeInMillis());
Log.d("s", "HRD: " + cal.getTime().toGMTString());
alm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
/*AlarmManager.INTERVAL_DAY,*/ pi);
}
}
As you can see I'm trying to just get it working one (hence the commented out part), however it doesn't work still.
Can anyone help?
Thanks,
Joe
I seem to have fixed this. For some reason Android doesn't like to fire Alarms if the receiver is a subclass.
To fix and keep origination, I moved it all to a new namespace and it worked.
Joe