Checking date every time in android - android

I am developing an app. The basic concept of the app is that I have to keep few video playing continuously. The list of videos i.e playist is maintained by webservice. The playlist has dates on which date which playlist is to be played. My problem is how to check the date and load playist accordingly? The videos from proper playlist should keep on playing. So do I need to keep a service in background which checks the date each day or what do i need to do?

Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
webservicecall();
}
}, 2000); // 2sec
this is an eg. to set time interval u can use it logically as u want

Use a specific function which gets called by itself after your required time and check the time and date condition if matches do the task..
if u wish a delay/ recursive call function can be provided..

Related

Triggering An Event At Certain Dates Android

i am currently developing an auction app. each item will have a time frame before the auction ends on that item. i was wondering if anyone can give me an idea on how to listen to each item and check if the auction time has ended on that given item.
should i use a Service and build in it a trigger every second that checks each item in the database?
thank you in advance.
What I did for a previous app to solve a similar issue was to include the date/timestamp of the end date, and then implement a handler to implement a countdown until that date/time.
UpdateDisplay();
handler.removeCallbacks(updateTimeTask);
handler.postDelayed(updateTimeTask, 1000);
}
private Runnable updateTimeTask = new Runnable() {
public void run() {
UpdateDisplay();
handler.postDelayed(this, 1000);
Log.i(TAG, "End of updateTimeTask");
}
};
And inside UpdateDisplay() I kept the code for updating the UI, based on some logic subtracting the current time from the time remaining to show a second hand ticking down.

AsyncTask refreshing google map api v2

My android application retrieves some json data from remote API for each Marker (a Marker shows the position of a real device, there are less than 10 devices to watch) present on the map, and sets status of a device by changing a color of the marker according to some rule working on a given json data. I use AsyncTask to fetch json data and change a status of a device. I keep fetched data in ConcurrentHashMap<Device, Data>. So, I run a number of asynctasks, one for each device. I also use a custom info window (in fact custom InfoWindowAdapter) to show some more data about device. First I draw a markers and keep them in a map HashMap<Device, Marker>. I execute asynctasks one by one using:
new MyAsyncTask(markerMap).execute(device)
My custom InfoWindowAdapter overrides getInfoContents method, where some collected by asynctasks data are used to be shown in InfoWindow, when clicked.
Everything works fine. But now I want to refresh my markers every 10 sec. I have tried to do it using the following approach:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
#Override
public void run() {
new MyAsyncTask(markerMap).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, device1);
new MyAsyncTask(markerMap).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, device2);
}}, 0, 10000);
My question is if it is the right/ best way to refresh my map? Or should I use Runnable instead of AsyncTask, and within a Runnable send a message to Handler when fetching json data to update Marker? One more question: should I use Timer or ScheduledExecutorService? I have read some StackOverflow discussions but I dont know the final recommendation. Thanks.
Finally I have solved my problem. In mean time I have rewritten part of my program in order to use ScheduledThreadPoolExecutor with subclassed Runnable (instead of Asynctask). In addition I have added onResume() method where I restart ScheduledThreadPoolExecutor using: mScheduledThreadPoolExecutor.scheduleAtFixedRate(runnable, 0, interval, TimeUnit.MILLISECONDS);

How to show an image for a specific time period?

In my application i need to decrypt the certain message format, to extract information like message id, timeout and so on.i need to show an corresponding image for the given id as well as to show it for the mentioned time period.
For that i have created one custom layout to show the image and other details. i'm using imageview for displaying the image. but dont know how to set timeiut for that?
Do anyone have idea on that?
You can easily use Handler to do that, like this
imageuser.setImageBitmap(bitmapObject);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imageuser.setImageBitmap(null);
}
}, 5000);
here, imageuser is your ImageView and replace 5000 with your specific time in miliseconds.
Just use it like whenever you want to show image just call your UI and start a thread for the given time you want to show the image and when the time complete just make that ui visibility gone,this is the logic try it in your own way.
thanks

Android pausing with thread but adding stuff to a text view

I have a linear layout in which I am placing a textview with the id "out". In my code i am getting this textview and calling out.append("Some string here");. What i want to do is have it use a thread.sleep(1000); to wait one second and then do another append. When i just use a for loop and iterate through it ten times it waits ten seconds and then updates the view at the end. How can i make this update the view in between the sleeps?
ps. The main reason for this is because i have another thread running with a bluetooth output stream and i want it to update the textview every time i send a byte to an arduino connected through a bluesmirf module. I can get it to send data but the updating of the screen happens at the end of the for loop. If i put a sleep in this loop it will wait the one second and then output to the arduino no problem. I just want to update it so i can see where things fail as they fail without using the logs.
Maybe you can use a postdelay handler
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//Here is the code you want to run after 1 second
}
}, 1000);
The handler is not blocking, but maybe it can help
Excuse me for my bad english, good luck!

Android - Get EditText and show dialog

I want my application can get user's input of time (HH:mm) from EditText widget.
Based on this time value my app needs to show a dialog when current time matches entered time.
Gaauwe
*Edit*
I want to place an EditText widget in my app.
A user will fill it with some time value (e.g. 10:30).
Then when real time (10:30) come up a dialog will be shown.
I think you can use the AlarmManager for this.
I d suggest you have a look at some tutorials like these to help you get started
http://michael.theirwinfamily.net/articles/android/android-creating-alarm-alarmmanager
http://android.arnodenhond.com/tutorials/alarm-notification
That is not too difficult. When user finished editing you EditText, read the time value and create instance of AlarmManager with start time calculated as difference between current time and whatever user wrote in the EditText. Better to use TimePicker to avoid parsing user`s input. Add receiver for you AlarmManager, receiver will start Service which will show dialog or do anything you want. You need to use AlarmManager because if your device is sleeping nothing will wake it up except system call like AlarmManager. #Zortkun 's post with links will help you to figure out how manage AlarmManager.
try this :
use the service : then when user enter time starts a service when system time and user entered time match the shows..
You can pull the data out of the EditText with:
findViewById(R.id.yourEditText).getText().toString();
The rest of your question I didn't understand.
RAW WAY!
So when user put text inside edittext and click button, you could save text in this way:
String time = findViewById(R.id.yourEditText).getText().toString();
and start a thread that check for time, and when time is equal to user's string time, you can show a dialog :)
Thread t = new Thread(new Runnable(){
public void run(){
while(new Date().getLocalTime()!=usersTime){ // is just pseudocode
Dialog.show();
}
}
});
I'll try to understand...
Seeing as you know how to pull the text from an EditText, you'll need an if statement.
Something that compares that time to the current time.
if (editTime == realTime) {
Toast.makeText(getApplicationContext(), "RING RING RING",
Toast.LENGTH_SHORT).show();
}
Use something like this:
Read this to figure out how to get a string of current time.

Categories

Resources