Write to the table every 5 minutes and always running - android

Goal:
I want to have a service that write a record every 5 minutes in the table of database.
The work done:
To accomplish the goal, an Alarmmanager was used that did this every 5 minutes. Unfortunately, after a while the Alarmmanager goes off (for android 8 or above).
public class SensorInfoAlarmManager {
public void setReminder(Context context) {
MsdSQLiteOpenHelper msdOpenHelper = new MsdSQLiteOpenHelper(context);
SQLiteDatabase dbInstanse = MsdDatabaseManager.getInstance().openDatabase();
int interval = msdOpenHelper.getSensorInfoInterval(dbInstanse);
Calendar calendar = Calendar.getInstance();
Calendar setcalendar = Calendar.getInstance();
setcalendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
setcalendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
setcalendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
cancelReminder(context);
ComponentName receiver = new ComponentName(context, SensorInfoAlarmReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Intent intent1 = new Intent(context, SensorInfoAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
android.app.AlarmManager am = (android.app.AlarmManager) context.getSystemService(ALARM_SERVICE);
am.setRepeating(android.app.AlarmManager.RTC_WAKEUP, (long) interval* 1000, (long) interval * 1000, pendingIntent);
}
}
It seams android does not allow the Alarmmanager to always be running.
I used from these solutions 1, 2 and 3, but no result found.
What is the problem? Is there a better solution?

Related

Dialy Alarm code Explanation

I am new to android , I want to make alarm that notify me dialy at 11am .I have found some code on net,But dont know some part of it. Below is code
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis()); does
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);
Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();
}});
can any one explain me what this two line does
calendar.setTimeInMillis(System.currentTimeMillis()); does
calendar.add(Calendar.SECOND, 10);
And also help me how can i set alarm that will notify me on the same time for example at 11am
Call this function where you need:
private void setDailyNotification(int ID, int hh, int mm, int ss) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(Dashboard.this, MyDailyReceiver.class);
alarmIntent.putExtra("ID", ID);
Log.d("setDailyNotification", "ID:" + ID);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
Dashboard.this, ID, alarmIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mEverydayPendingIntent = pendingIntent;
Calendar calendar = Calendar.getInstance();
Calendar now = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hh);
calendar.set(Calendar.MINUTE, mm);
calendar.set(Calendar.SECOND, ss);
// check whether the time is earlier than current time. If so, set it to
// tomorrow. Otherwise, all alarms for earlier time will fire
if (calendar.before(now)) {
calendar.add(Calendar.DATE, 1);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
mEverydayPendingIntent);
Log.d("setRepeated", "ID:" + ID);
}
Broadcast receiver:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
mBuilder.setContentTitle("Daily Summery");
mBuilder.setContentText("Today's Transaction");
mBuilder.setSmallIcon(R.drawable.app_icon);
Log.d("tag1234", "In if" + daily_Reminder);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
mBuilder.setStyle(inboxStyle);
mNotificationMa = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationMa.notify(11, mBuilder.build());
call function like this:
setDailyNotification(11, 11,00, 00);
System.currentTimeMillis()- gets the current time from the device.
calendar.setTimeInMillis()- sets the current time.
calendar.add(Calendar.SECOND, 10) - adds ten seconds to the current time.
In total, you are fetching the current time of the device and adding ten seconds in it.
You can try this Worked for me
int interval = 1000 * 60 * 1;
// 1 for minutes ,you can pass 60*24
//And set alarm manager like this
Intent alarmIntent = new Intent(AlaramManagerActivity.this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlaramManagerActivity.this, RQS_1, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),interval, pendingIntent);
Intent myIntent = new Intent(activity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar firingCal= Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();
firingCal.set(Calendar.HOUR,11); // At the hour you wanna fire
firingCal.set(Calendar.MINUTE, 0); // Particular minute
firingCal.set(Calendar.SECOND, 0); // particular second
long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();
if(intendedTime >= currentTime){
// you can add buffer time too here to ignore some small differences in milliseconds
// set from today
alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
}
else{
// set from next day
// you might consider using calendar.add() for adding one day to the current day
firingCal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingCal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);}

Alarm is not Fired when i set time in android

public class SchedulerSetupReceiver extends WakefulBroadcastReceiver {
private static final String APP_TAG = "com.hascode.android";
private static final int EXEC_INTERVAL = 10 * 1000;
DBhelper dbhelper;
#Override
public void onReceive(final Context ctx, Intent intent) {
Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called");
dbhelper = new DBhelper(ctx);
String[] ID = dbhelper.FetchAllID();
String[] time = dbhelper.FetchAlltime();
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
AlarmManager alarmManager = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR_OF_DAY, 16);
calendar.set(calendar.MINUTE, 22);
calendar.set(calendar.SECOND, 0);
calendar.set(calendar.MILLISECOND, 0);
long sdl = calendar.getTimeInMillis();
for (int k = 0; k < ID.length; k++) {
intent = new Intent(ctx, SchedulerEventReceiver.class);
// Loop counter `i` is used as a `requestCode`
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, k,
intent, 0);
// Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, sdl,
pendingIntent);
intentArray.add(pendingIntent);
}
}
This is My code for Alarm i want to set Alarm for particular time But its not trigger when time completed i have use calendar and i have set to fire 4:20 minute Pm for that i have add in Hour 16 and in minute 20 but its not triggered when Device time reach 4.20 Pm please tell me what i doing mistake
Try this
alarmManager.set(AlarmManager.RTC_WAKEUP, sdl,
pendingIntent);
Make sure that you have registered the broadcast receiver in Manifest.
<receiver android:name=".SchedulerEventReceiver"/>
And provide alarm permission as well.
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
I think you forgot to the tell the Calendar what day is today.
calendar.setTimeInMillis(System.currentTimeMillis());

Android AlarmManager to Trigger After One Hour

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

Alarm does not make itself visible

In monodroid I want to set an alarm. I would like to have a function I can pass a seconds to and the function creates the alarm at the current time + seconds.
All examples I found were nested in some other projects. I am looking for a general purpose solution.
I found
AlarmManager am = (AlarmManager)getSystemService(alarm);
Intent i= new Intent("MY_INTENT");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 2);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
and changed it to
AlarmManager am = (AlarmManager)GetSystemService(AlarmService);
Intent i = new Intent("MY_INTENT");
PendingIntent pi = PendingIntent.GetBroadcast(this, 0, i, 0);
seconds = Convert.ToInt16(txtMinutes.Text) * 60;
am.Set(AlarmType.ElapsedRealtimeWakeup,SystemClock.ElapsedRealtime() + (seconds * 1000), pi);
But after seconds passed, literally nothing happens.
What to change?

Android AlarmManager not firing at set time

I have an AlarmManager that is supposed to fire off on the 26th of December 2013. Here is my code:
FestCountdownTimer countdownNotificationTimer = new FestCountdownTimer(
00, 00, 9, 26, 11, 2013);
long timeToEvent = countdownNotificationTimer.getIntervalMillis();
System.out
.println("TIME TO EVENT!!! ------------------ " + timeToEvent);
Intent eventAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, timeToEvent, PendingIntent
.getBroadcast(this, 1, eventAlarm,
PendingIntent.FLAG_UPDATE_CURRENT));
But, my alarm is firing off after 1 or 2 minutes. I don't know what seems to be wrong. (Byt the way, the FestCountdownTimer is a class I made to get the interval between now and the future set date. The long timeToEvent; is coming properly.)
Thanks for the help...
I dont know about your FestCountdownTimer class but damn sure
probablay time not set properly(I mean may be mistaken convert time to
millisecond)
Note: This below code is harcode not tested by in actual app
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
cal.set(year,month,day,hour,min); //set your date here
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// cal.add(Calendar.SECOND, 5);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

Categories

Resources