Alarmmanager or Alarmclock - java

I want to program an app that has two independent, repetitive alarms. There are two classes seem to be able do this: AlarmManager and AlarmClock. I've tested AlarmManager, but when Android restarts all alarms are cleared.
Which should I use?

Use BroadcastReceiver to handle Android OS boot broadcast and reschedule your alarms.

AlarmManager services allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running.
You may find the SO post helpful Android AlarmManager
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);
Whereas AlarmClock provider contains an Intent action and extras that can be used to start an Activity to set a new alarm in an alarm clock application.

Related

How can I poll a device for location data quicker than WorkManager's 15 min cap?

I am trying to poll a device for long/lat every minute or so... I have been researching and found a few things. I was thinking of using:
PeriodicWorkRequestBuilder() - however, I have read that it can only execute every 15 minutes at a minimum...
What tech/design pattern are available to me, if I wanted to poll for data in a shorter period?
Are foreground services my only choice?
You can Use any Runnable but when app closed it will not working .
Foreground service is best choice but in android 9+, it will confuse the user
this is the link that show to you how use it:
Timer
BUT
I advice you to use an AlarmManager that work like work manager but it response better, you can Use Countdown timer for it, link below will help You to use it:
AlarmManager
Edited:
sample for open activity with alarm manager
Intent intent = new Intent(this, YOUR_MAIN_ACTIVITY.class);
intent.setAction(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, ID,
intent, 0);
final long DELAY_IN_MILLIS = DELAY_IN_MILLI_SECONDS+
System.currentTimeMillis();
AlarmManager alarmManager = (AlarmManager)
getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, DELAY_IN_MILLIS,pendingIntent);
and there is a full example for you:
Example

AlarmManager not working in Android 8.1.0

My requirement is set Notification for specific time like user birthday or holiday
I am using AlarmManager for scheduling notification using broadcast receiver
Code is working fine in 6.0 (even when app is killed,swiped from recent list) but not working on Android 8.1.0 (Mf : Oppo)
Readed This and This and many answers but not find any helpfull
any idea how to solve this issue
Here is my code
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
//creating a new intent specifying the broadcast receiver\
Intent i = new Intent(this, HolidayBroadcast.class);
i.putExtra("eventName",islamicHoliday.getEventName());
i.putExtra("dateH", testTmp.getCalendar().getTimeInMillis());
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
//creating a pending intent using the intent
PendingIntent pi = PendingIntent.getBroadcast(this, new Random().nextInt(), i, PendingIntent.FLAG_UPDATE_CURRENT);
//setting alarm
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,getTimeInMillis(), pi);
}
else
{
am.setExact(AlarmManager.RTC_WAKEUP, getTimeInMillis(), pi);
}
You can use workmanager as mentioned in link.It supports all api version and easy to use because it uses backward compatibility to API level 14
https://developer.android.com/topic/libraries/architecture/workmanager
https://medium.com/androiddevelopers/introducing-workmanager-2083bcfc4712
If your task is time critical, WorkManager API is not recommended as for your alarm manager not working properly, on Android 8 use
AlarmManager.setexactandallowwhileidle(), to set exact time and allow wakeup notification while app is killed.

how to wake up my intent service every 5 minutes [duplicate]

This question already has answers here:
Start Android Service after every 5 minutes
(3 answers)
Closed 7 years ago.
I know this question is asked before, but I didn't get any answer, I want to create an intent service that run a thread all the time, but when I get out from the app my service is stopped then the thread stopped too. I need to create something to wake up the service every some minutes. or something to prevent killing the service even when app is killed or closed.
this is how I start my service
Intent intent= new Intent(Intent.ACTION_SYNC,null,this,IntentServ.class);
startService(intent);
For this you can use the AlarmManager that could start your service for every 1 Hour. For Example :
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context,
UpdateService.class);
PendingIntent pendingIntent=PendingIntent.getService(context, requestCode, Intent.parseIntent(), 0);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), AlarmManager.INTERVAL_HOUR, pendingIntent);
AlarmManager and PendingIntentare what you need in this case.
Follow below example :
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
/* --- Create pending intent, which will be executed when wake-up --- */
PendingIntent operation = getUpdatePolicyOperation();
am.set(AlarmManager.RTC, alarmTime, operation); // alarm time is millisecond time in milliseconds that the alarm should go off, using the appropriate clock (depending on the alarm type).
You can read more about alarm mode in AlarmManager in Here or take a tutorial in Here
Hope it's helped.
Use normal Service with android.app.AlarmManager.
Don't need to use WakefulBroadcastReceiver.

Slower android when not connected to charger?

I'm trying to develop simple android application which monitors specified url using http client and once defined condition is met it should perform notification actions.
I have one activity which starts separate thread and put reference to it via static value. (When activity is recreated I'm chechking reference for not null to determinate if child thread was already started). In this child thread I have while loop which gets json data from url and parse it.
I've noticed weird behavour (maybe because I'm android dev newbie). Once application is in foreground it works quite fast, when android device goes into sleep mode it doesn't perform requests to often. (maybe some energy safe policy?). What's the most weird is that once I connected phone to computer via usb cable to works fast (even when application is in background and phone has black screen).
Is there any relationship to activating /disactivating applications based on connected charger?
I can't debug it because once I connected cable it works fine, and I can't debug without being connected to computer.
The matter probably is that phone goes to sleep mode when it stops almost all activity and slows down CPU. It is used to save battery. Timers on Handler.postDelayed() for example won't work properly (not called on time).
There's special concept for this matter - for activities that needs to be performed in sleep mode, you need to use AlarmManager, see Scheduling Repeating Alarms
The matter is that your app needs to register with AlarmManager, and then it will receive scheduled events when phone wakes up from sleep mode. Your app needs to get lock with PowerManager to perform activities (in your case it's downloading JSON from network), which you do not want to be interrupted with sleep mode while you're executing them. Consider this example:
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
/**
* This method is called when we are waking up by AlarmManager
*/
#Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "pulse app");
//Acquire the lock
wl.acquire();
//You can do the processing here.
//Release the lock
wl.release();
}
/**
* Register our app with AlarmManager to start receiving intents from AlarmManager
*/
public static void setAlarm(Context context)
{
int interval = 10; // delay in secs
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval*1000 , pi);
}
/**
* Unregister the app with AlarmManager, call this to stop receiving intents from AlarmManager
*/
public static void cancelAlarm(Context context)
{
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
This code needs android.permission.WAKE_LOCK permission in Manifest file.
Another post about AlarmManager usage: Android: How to use AlarmManager
And this: prevent mobile from going into sleep mode when app is running
Article at the first link says that it's preferable to use Sync Adapters for such purpose, but I haven't used them myself.

Android Service killed by android V 2.3 (gingerbread)

I have a GPS service whose work is to fetch the coordinate into a server. This service suppose to be run 24/7. but it is somehow killed in between.
This is happing only in android v 2.3. On android v2.2 it's running fine.
In this service , i am using "LocationManager" and it's method "requestLocationUpdates" which is creating a loop.This loop is responsible for fetching the coordinates. So my goal is to keep the loop running.
So what to do, to make by service run 24/7.
This server suppose to be run 24/7
You cannot do that. This is not possible in any real sense of the term, as you have discovered. It is also not a good design choice at all.
If you absolutely need it to be running all the time, you need to
acquire a PARTIAL_WAKE_LOCK via PowerManager. This will keep the CPU
on all the time, and your program running. Be prepared for a shocking
decline in battery life.
Instead use AlarmManager. You can schedule a PendingIntent via the AlarmManager thats starts the service at the relevant point in time to do it's work. When done, kill the service again.
Below is a sample code showing how the AlarmManager is used which will launch the intent to start YourService in 5 minutes from now:
// get a calendar with the current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 5);
Intent intent = new Intent(ctx, YourService.class);
PendingIntent pi = PendingIntent.getService(this, 123, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
Please use repeating Alarm Manager which start your service after a given time period
private void setAlarm() {
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), LocationUpdateService.class);
intent.putExtra("locationSendingAlarm", true);
PendingIntent pendingIntent = PendingIntent.getService(this, AppConstants.PENDING_INTENET_LOCATION_SENDING_ALARM_ID, intent,0);
try {
alarmManager.cancel(pendingIntent);
} catch (Exception e) {
}
int timeForAlarm=5*1000*60; // 5 minutes
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+timeForAlarm, timeForAlarm,pendingIntent);
}

Categories

Resources