set alarms from sqlite db in java android - java

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

Related

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")

Dynamic notification from sqlite db

I've got a question for you. I want to use a notification service through BroadcastReceiver and AlarmManager. I set the alarm manager with a dynamic list of times from my db.
I use this method that i call on my MainActivity OnCreate(), but doesn't work
private void restartNotify() {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(pendingIntent);
ArrayList<String> item = new ArrayList<String>();
item = GetLists.GetTimesListForNotification(this);
for (int i = 0; i < item.size(); ++i) {
String time = item.get(i);
int Position = time.indexOf(":");
int hour = Integer.parseInt(time.substring(0, Position));
int min = Integer.parseInt(time.substring(Position + 1));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
am.set(AlarmManager.RTC, cal.getTimeInMillis(),
pendingIntent);
}
this is my broadcast receiver
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager mManager;
mManager = (NotificationManager) context.getApplicationContext()
.getSystemService(
context.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context.getApplicationContext(),
MainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,
"Nuovo messaggio da cura alert", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
context.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context.getApplicationContext(),
"Le notifiche funzionano", "it is working",
pendingNotificationIntent);
mManager.notify(0, notification);
}
}
Thank you.
Well this is because your pending intents request code is same though you are running a loop. What you can do is get your pending intent intu your loop and set the request code i. This may work. Though it is late but this may help others

How to create a custom notification in android

I would like to create a custom notification service in my app.
I wouldn't use the google service (google cloud messaging).
Is there a way to create a daemon that check every X seconds a particular condition on my app db and show a notification?
Thank you,
edit
I call this method in MainActivity's oncreate(). I get the times from my db
private void restartNotify() {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(pendingIntent);
ArrayList<String> item = new ArrayList<String>();
item = GetLists.GetTimesListForNotification(this);
for (int i = 0; i < item.size(); ++i) {
String time = item.get(i);
int Position = time.indexOf(":");
int hour = Integer.parseInt(time.substring(0, Position));
int min = Integer.parseInt(time.substring(Position + 1));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
am.set(AlarmManager.RTC, cal.getTimeInMillis(),
pendingIntent);
}
And this is my broadcast receiver
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager mManager;
mManager = (NotificationManager) context.getApplicationContext()
.getSystemService(
context.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context.getApplicationContext(),
MainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,
"New message to read", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
context.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context.getApplicationContext(),
"notification are work", "it's work",
pendingNotificationIntent);
mManager.notify(0, notification);
}
}
You can use AlarmManager + BroadcastReceiver like so:
private void setRecurringAlarm(Context context) {
Intent downloader = new Intent(this, MyStartServiceReceiver.class);
downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, downloader, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 6000, 10000, pendingIntent);
}
BroadcastReceiver class:
public class MyStartServiceReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//do the stuff...
}
}
And don't forget to register receiver in your manifest:
<receiver android:name=".MyStartServiceReceiver"
android:enabled="true"/>
You have to use Alarm Manager to schedule code for execution every specific period. take a look here:
http://developer.android.com/training/scheduling/alarms.html

AlarmManager Doesn't work

This is my code. This code worked fine until yesterday. MyAlarmManager is correctly called (i can see the logs), but MyReceiver is not called. Do you know why?
public class MyReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//It will start the application at a certain time
Intent i = new Intent(context, Calibration.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Log.e("MyReceiver", "MyReceiver");
}
}
And this is the AlarmManager class
public void onCreate()
{
//Start
Intent myIntent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pref = getApplicationContext().getSharedPreferences(
"settings", MODE_PRIVATE);
int h=pref.getInt("hour", -1);
int m=pref.getInt("minute", -1);
Calendar start=null;
Calendar start2=null;
//If the user added a time in the options activity, the application will start at that time
if(h!=-1 && m!=-1)
{
if(pendingIntent != null)
{
alarmManager.cancel(pendingIntent);
}
Log.e("h e m", h+" "+m);
GregorianCalendar now = new GregorianCalendar();
start = new GregorianCalendar();
start.set(Calendar.HOUR_OF_DAY, h);
start.set(Calendar.MINUTE, m);
start.set(Calendar.SECOND, 0);
if(h>=0 && h<12)
start.set(Calendar.AM_PM,Calendar.AM);
if(h>=12 && h<=23)
start.set(Calendar.AM_PM,Calendar.PM);
if(start.after(now))
{
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, start.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
alarmManager.set(AlarmManager.RTC, start.getTimeInMillis(), pendingIntent);
Log.e("MyAlarmManager", "MyAlarmManager");
}
}
//Start 2
Intent myIntent2 = new Intent(this, MyReceiver.class);
pendingIntent2 = PendingIntent.getBroadcast(this, 2, myIntent2,0);
int h2=pref.getInt("hour2", -1);
int m2=pref.getInt("minute2", -1);
//If the user added a time in the options activity, the application will start at that time
if(h2!=-1 && m2!=-1)
{
if(pendingIntent3 != null)
{
alarmManager.cancel(pendingIntent2);
}
Log.e("h2 e m2", h2+" "+m2);
GregorianCalendar now = new GregorianCalendar();
start2 = new GregorianCalendar();
start2.set(Calendar.HOUR_OF_DAY, h2);
start2.set(Calendar.MINUTE, m2);
start2.set(Calendar.SECOND, 0);
if(h2>=0 && h2<12)
start2.set(Calendar.AM_PM,Calendar.AM);
if(h2>=12 && h2<=23)
start2.set(Calendar.AM_PM,Calendar.PM);
if(start2.after(now))
{
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, start2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent3);
alarmManager.set(AlarmManager.RTC, start2.getTimeInMillis(), pendingIntent3);
Log.e("MyAlarmManager3", "MyAlarmManager3");
}
}
}

Alarm Manager is running immediately

Date dat = new Date();
Calendar cal_alarm = Calendar.getInstance();
Calendar cal_now = Calendar.getInstance();
cal_alarm.setTime(dat);
cal_alarm.set(Calendar.HOUR_OF_DAY, hrs);// set the alarm time
cal_alarm.set(Calendar.MINUTE, min);
cal_alarm.set(Calendar.SECOND, 0);
cal_alarm.set(Calendar.MILLISECOND, 0);
if (cal_alarm.before(cal_now)) {// if its in the past increment
cal_alarm.add(Calendar.DATE, 1);
}
Intent intent = new Intent(ctx, AlarmReceiver.class);
// intent.putExtra("Reminder to Take Photo", "Pixitch!");
PendingIntent sender = PendingIntent.getBroadcast(ctx, 0010000,
intent, 0);
// Get the AlarmManager service
long tmemills = cal_alarm.getTimeInMillis()
- cal_now.getTimeInMillis();
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, tmemills,
AlarmManager.INTERVAL_DAY, sender);
Alarm Receiver Class
public class AlarmReceiver extends BroadcastReceiver {
private static final int MY_NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private Notification myNotification;
// Context ctx = this;
#SuppressWarnings("deprecation")
#Override
public void onReceive(Context context, Intent intent) {
try {
NotificationManager mNM;
mNM = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.ic_launcher, "Pixitch Notification !",
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, AlarmManage.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(context, "Pixitch Notification!",
"Reminder For TakePhoto", contentIntent);
mNM.notify(0, notification);
} catch (Exception e) {
Toast.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
the value of tmemills is 278,088
the tmemills is around 4.5 minutes but
the alarm Manager is Running immediately
I am not able to find where the problem is because I am beginner for Android. please help me
Try this:
am.setRepeating(AlarmManager.RTC_WAKEUP, tmemills + System.currentTimeMillis(),
AlarmManager.INTERVAL_DAY, sender);
Get rid of tmemills. Use cal_alarm.getTimeInMillis() as the second parameter to your set() call on AlarmManager, as that is the number of milliseconds since the Unix epoch at which you want the event to occur.

Categories

Resources