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.
Related
I have asked a question before 2 days about daily notification and i put my code and i didn't get any useful answer so i need someone write a code that show notification every 1 hour .
caution (i have write a code doing that but i was have a problem that is every time i open the activity it gives me a notification i don't need that i just want it give a notification at the first time i run app or press a button to start it then the service runs to give notification every 1 hour ) .
Hope anyone can help me .
Thanks.
Well this seems like a two part question, for one the effect that you want to wait and be able to check to start the notifications and to save that value. SharedPrefence's can do this, you could save a boolean value to see whether or not a alarm should be set. Easy enough to access.
public boolean saveSharedBoolValue(String key, Boolean value, Context localContext) {
SharedPreferences sp = localContext.getSharedPreferences("FILE_NAME", 0);
Editor edit = sp.edit();
edit.putBoolean(key, value);
return edit.commit();
}
public Boolean getSharedBoolValue(String key, Context localContext)
{
SharedPreferences sharedPreferences = localContext.getSharedPreferences("FILE_NAME", 0);
Boolean value = sharedPreferences.getBoolean(key, false);
return value;
}
Something like this. With alarm notifications, that start on startup I had this similar issue. What I noticed was that I was creating an alert for earlier than my present time and it created a single notification automatically.
That may not be your issue though,
calendar.setTimeInMillis(System.currentTimeMillis() +5*1000);
this line from your code. Isn't it essentially creating an alarm just right after it starts? 5000 milli seconds aren't much after present time.
I want to create a condition to wait for a broadcast upon a button press
right now I am just doing solo.sleep(10000)
but I dont want to sleep solo for nothing
How do I formulate the condition "broadcast received" ?
Ok explanations
Robotium Solo is an instrumentation framework with nice api
It has a method called "solo.waitForCondition(Condition, int timeout)"
I want to formulate (the word formulate means say what i want to say in correct words)
the correct condition that will tell me that the broadcast was indeed received
I want to write some code (I don't know which exactly) to know that the broadcast was indeed sent
for example, if i want to know that a button is now visible i would write
solo.waitForCondition(new Condition(){
public boolean isSatisfied(){
Button b = getActivity().findViewById(R.id.myButton);
return b.getVisibility() == View.VISIBLE;
}
}
now back to my question - What (not how, but what) do I write in order to know for sure that the broadcast was sent inside the isSatisfied method
I suppose you meant that you don't want to sleep for 10 seconds, if you get the broadcast earlier. What you can do is
long beginTime = new Date().getTime();
while (new Date().getTime() - beginTime < 10000) {
solo.sleep(500);
if (conditionMet) {
// Do something
break;
}
}
This way you can do these checks on smaller intervals.
Ok, so in fact this is more or less how waitForCondition is implemented. Unfortunately I don't think you can listen for events with robotium. What you can do is monitor the view hierarchy. In your case, there should be some difference to the views that is triggered when the button is clicked, so that is what you need to check for in the Condition (and your example does that).
This is if you don't want to edit the code you are testing. If you are willing to change the code, you can add an onClickListener() and in that you can set a view's Tag to a boolean for example. Later in robotium you can check for that tag for being set. This is however not good way to do it, because you are adding more code just for the sake of the tests.
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
This question is about TimePicker behavior in Android.
To get the value of the Hour and the Minute I use getCurrentHour() and getCurrentMinute().
At first I found out that changes made by the user using the phone keyboard where not registered so calling getCurrentHour() or getCurrentMinute() did not show the values changed in the texboxes inside the widget.
This was quickly solved using setAddStatesFromChildren(true); With this I get those changes.
Now, my problem is that those changes are only registered if timepicker looses focus.
So, getting the value from timepicker inside a button gets a wrong value because it hasn't loose focus.
Any help?
What I need is: Can I force a timepicker to loose focus and get the real written value in text boxes?
I solved it and I write it here for someone who needs it.
Thanks to "Tarun Maheshwari" I found what I needed:
On onCreate do something like this:
MyTimerPicker.setAddStatesFromChildren(true);
On the onClick method, when getting the values, do this:
MyTimerPicker.clearFocus();
int iHour = MyTimerPicker.getCurrentHour();
int iMinute = MyTimerPicker.getCurrentMinute();
Call clearFocus() on the timepicker view after you get onTimeChanged callback.
Can someone please show me a code example about how to get a long click (2 sec for example) on the volume up hardware key?
Thanks :)
EDIT
The class that i want to capture the long click with is a Service. How can i do that?
If you just need to capture long clicks, this answer might be helpful:
https://stackoverflow.com/a/5269673/1401257
EDIT:
I have never tried to have a key listener inside a service, but with a little help from Google I found this: Volume change listener?
It seems that normal key events can only be handled from Activities. I do not have time to try this out myself, but for capturing long clicks it might be possible to combine the answer from the link and Lukes answer.
From what I understand about BroadcastReceivers, you would want to create a receiver, that notify the Service whenever someone click the volume buttons.
Optionally you could do something like this:
if(clickedDown) {
if(beginningTime + 2000 < System.currentTimeMillis()) {
// Ok, the button has been clicked down for 2 seconds
}
}
else {
beginningTime = System.currentTimeMillis();
}
Applying something like this, you'll be able to define the amount of time to wait.