How to repeat the task using alarm Manager in Android - java

I want to repeat the task at specific time like 9:45, 10:10
AlarmManager alarmMgr0 =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent0 = new Intent(this,schedule.class);
PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0,
intent0, 0);
// Intent intent0 = new Intent(this, OldEntryRemover.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 32);
calendar.set(Calendar.SECOND, 0);
//set that timer as a RTC Wakeup to alarm manager object
alarmMgr0.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent0);

Try this:-
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
References:-https://developer.android.com/training/scheduling/alarms

Related

set alarms from sqlite db in java android

I'm trying to make alarms using the times provided in the sqlite database for the reminders. however I can't seem to be getting it right. the code I have so far is: (in my mainactivity oncreate)
for (int i = 0; i < allRems.size(); i++) {
int mHour = 0, mMin = 0;
String mAlarmTime = allRems.get(i).getRemTime();
int mID = allRems.get(i).getRemId();
if (!(mAlarmTime == null)) {
String[] time = mAlarmTime.split(":");
mHour = Integer.parseInt(time[0].trim());
mMin = Integer.parseInt(time[1].trim());
}
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, mHour);
calendar.set(Calendar.MINUTE, mMin);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, mID, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
and my alarm receiver class is
#Override
public void onReceive(Context context, Intent intent) {
Log.e("onReceive", "--------------------------------------------------------");
Toast.makeText(context, "OnReceive alarm test", Toast.LENGTH_LONG).show();
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Log.e("DEVICE: ", "Device Rebooted");
}
}
i want to be able to set reminders for the specific time for the reminders in the database

Reset integer daily, in specific time

I need to reset an integer "daily" at a set specific time of day. In my activity, I write the value of daily into Shared preferences, then with an alarm manager and broadcast receiver try to set it to zero. everything works, besides the shared preferences and the value still remains what it was. I think that I need to import a different context for the shared prefs. Any help is welcomed.
The receiver:
public void onReceive(Context context, Intent intent) {
final SharedPreferences shared = context.getSharedPreferences("Mydata", MODE_PRIVATE);
final SharedPreferences.Editor editor = shared.edit();
int sum = shared.getInt("data1", 0);
Calendar c = Calendar.getInstance();
String date = String.valueOf(c.get(Calendar.DATE));
mainData.insert_data(date, sum);
int zero = 0;
editor.putInt("data1", zero);
editor.apply();
}
The alarm manager:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 22);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
final Context context = this.getContext();
PendingIntent pi = PendingIntent.getService(context, 0,
new Intent(context, MyAppReciever.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
The Shared preferences in the Main Activity:
final SharedPreferences shared = this.getContext().getSharedPreferences("Mydata", MODE_PRIVATE);
final SharedPreferences.Editor editor = shared.edit();
editor.putInt("data1", 0);
editor.putInt("month", 0);
final int today212 = shared.getInt("data1", 0);
final int mesec212 = shared.getInt("month", 0);
final int limit212 = shared.getInt("budget", 0);
So I made a few changes to the code and added a PendingIntent requestCode.
The code looks like this
The place where you set up your Alarm:
public void scheduleTestAlarmReceiver(Context context) {
Intent receiverIntent = new Intent(context, MyAppReciever.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 123456789, receiverIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 58);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, sender);
}
and the place where you schedule it (say onClick):
Context cn= this.getContext();
scheduleTestAlarmReceiver(cn);
the Broadcast receiver should be the same as above.
And also do not forget to add the receiver to the manifest:
<receiver android:name=".MyAppReciever"></receiver>

Android : Set multiple repeating alarms

I am new to android programming. I want to set different repeating alarms. I wrote a code, but it is not working. It is not generating any alarms. Please help me.
Here is my code
public void setAlarm(int hour, int min,String am_pm){
if(hour == 12 && am_pm.equals("am")){
hour=00;
}
if(hour != 12 && am_pm.equals("pm")){
hour=hour+12;
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
Intent intent = new Intent(getBaseContext(), AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), (int) System.currentTimeMillis(), intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
For above I am passing setAlarm(8,30,"am")

Android: How to create a reminder such as google keep reminder

I want to add a reminder to my android app such as Google Keep app reminder for notes, as you know in Gkeep you can set reminder for your notes by set date (Today,Tomorrow,Next same day or Pick a date from Calendar) and set time (Morning, Afternoon,Evening, Night or pick a time from Clock).
Any help?
I found my answer! and share it with you because i think its nothing to do with Mechanical Turk :)).
in MainActivity:
final AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
final Intent intentAlarm = new Intent(MainActivity.this, AlarmReceiver.class);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 22);
calendar.set(Calendar.HOUR, 5);
calendar.set(Calendar.AM_PM, Calendar.PM);
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 6);
calendar.set(Calendar.YEAR,2015);
long when = calendar.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP,when, PendingIntent.getBroadcast(MainActivity.this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
and the AlarmReciver class :
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Calendar now = GregorianCalendar.getInstance();
Notification.Builder mBuilder =
new Notification.Builder(context)
.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("my title")
.setContentText("my text");
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.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());
}
}
And to cancel the reminder :
if (alarmManager!= null) {
alarmManager.cancel(PendingIntent.getBroadcast(MainActivity.this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}

AlarmManager Set Specific Time Execution

I was having some problem when trying to set specific time to execute the AlarmManager. Here is the code where I schedule my Alarm:
public static void scheduleAlarms(Context context) {
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 00);
cal.set(Calendar.MINUTE, 30);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context, Alarm.class);
notificationCount = notificationCount + 1;
notificationIntent.putExtra("NotifyCount", notificationCount);
PendingIntent pi = PendingIntent.getBroadcast(context, 1,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
I am trying to set the alarm manager to run at 0030 if it meets the conditions in my if statement. However, the alarmManager will not send push-notification although some records did met the requirements. So I changed the codes to repeat every 2 minutes by:
mgr.setRepeating(AlarmManager.RTC_WAKEUP, System.getTimeInMillis(),
2*60*60, pi);
And it did send the push-notifications correctly. I wonder what's the mistake I made for the part when I tried to specified a time for it to execute.
Thanks in advance.

Categories

Resources