How can I change my existing Thread.sleep() to alarm manager - android

Right now I use thread.sleep(3000) and print the data in log for every 3 seconds but now I want to change it to 15 minutes.
WHats the best to use?
Is it alarm manager or handler ?

What's wrong with:
thread.sleep(1000*60*15)

Related

Android: which kind of Broadcast/Receiver combination should I use?

In my app, I want to kick off an timer that triggers an action every x minutes, regardless if the user is currently in the app or not. I have been reading around and am not sure which combo of Broadcast and Receiver types I should use - any guidance would be helpful.
Example of user actions:
User hits a button, sets initial timer (alarm)
Timer is reached, trigger an action and set the timer again
Repeat until it has run for x minutes
when the user hits button set alarm as
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setRepeating (AlarmManager.Type,
long triggeringtime,
long interval,
PendingIntent operation);
here triggeringtime is how to time it shud take to take an action
and interval how to much time it would take to do the same.
here operation is the Intent which you need to execute like it may be an activity or Service you can Define it as
operation = PendingIntent.getActivity( context, 0, intent or service, 0);
2 and 3 step will be continuously run thats what the alarm manager does.
This alarm continues repeating until explicitly removed with
cancel(AlarmManager.OnAlarmListener).
I am a beginner Sry if i wrong. Hope it helps!
Android JobScheduler
You can find a lot of tutorial online.

Change the length of the Android Alarm

The android alarm duration is basically at 1min (I think).
I would like to change it, so I can set the length/the duration at 4min for exemple (the duration of a song).
Can you tell me which methods I can call to change it ?
Can you guide me please ?
Thank you in advance !
I think you can use AlarmManager for this. http://developer.android.com/reference/android/app/AlarmManager.html
It has setRepeating method where you can set the interval you want (the second parameter is time in milliseconds that the alarm should first go off).

Android program to: Display random image for 90 seconds then switch

I'm new to android. I need to make a program that is very basic. It needs to display a random image from my drawing folder. I can do that part because frank n stein posted How To Display Random images on image view
But I need it to stay on the screen for 90 seconds (and count down) or until a user clicks a button. Can someone help? I would really appreciate it. If you post an answer please assume I know absolutely nothing...barely the basics. But I'm trying to learn. :)
You can use a for loop with Thread.sleep() or a CountDownTimer or AlarmManager or a Handler with postDelayed option.
You can use android alarm manager with broadcast receiver,you will set alarm +90 second to this time and receiver the alarm or you must to use timer.But timer is spending battery more than alarm manager.
You can follow this tutorial for alarm manager and i think you must follow :)
http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/

Change brightness on specified hours - twice a day

I have an array of specified hours for each day, when brightness should by changed for application which is awake 24/7. It happens twice a day.
What I want to achieve is to find the most economical way of implementation that problem.
Should I use AlarmManager or maybe there is a better sollution?
Activity is always awake...
If your app doesn't show any UI while it's awake, use the AlarmManager.
Finally, I decided to use an AlarmManager with BroadcastReceiver setting value in SharedPreferences and then in Activity I have a Handler which checks that value.

What is the best way to schedule a task guaranteed to run at near-scheduled time in Android?

I have been using Timer & TimerTask with Pseudocode:-
samplingTask = new TimerTask() {
public void run() {
collectSample();
}
};
timer.schedule(samplingTask, 60*1000, 60*1000); //1 min
This application is a long running app (e.g. say 15 minutes without wakelock, and the screen turned off). After scheduling this task, I lock the screen and the phone is in motion (with sensor manager activated for accelerometer). So, ideally this task should run every minute. However, in the logs, I see that the scheduled timer was run at 5th minute, 9th minute and 12th minute only.
FINER: (13,Timer-0,Workout)In Timertask, nth minute=5
FINER: (13,Timer-0,Workout)In Timertask, nth minute=9
FINER: (13,Timer-0,Workout)In Timertask, nth minute=12
My requirement has time-critical sampling with an acceptable delay range of few seconds (1-5 seconds).It appears that if the device is in sleep mode, timer doesn't wake it up and run the scheduled task. Is there any other alternative to this. Documentation says AlarmManager and Handler can be used. I think AlarmManager should ideally be used to do a single shot task based on some system alarm. Is there something I am missing or some alternative to achieve this functionality.
You can use AlarmManager - it has opportunity to weak up device and run some task repeatedly.
I don't know exactly, but suppose, when user switches off the screen all tasks paused and can't do any code, while device sleep.

Categories

Resources