Android - Getting volume button long clicks - android

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.

Related

Robotium Solo - wait for broadcast

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.

Which is better approach for OnClick Implementation?

For implementation of onClick function which approach is better?
Saving touch start / touch up coordinates and processing this values for closeness? Like, if starting point and up point close each other, let the click action start.
Saving touch start / touch up time difference and processing this value? Like, if touch starting time and up time difference less than a value, let the click action start.
And why?
Depends on how many kind of touch events you want to support:
on Up : click
on Up : without moving much - > click , moved -> swipe
on Up : short duration - > click , long duration -> long press has
been triggered, ignore.
on Up and long press triggered : without moving much - > ignore , moved -> drag n drop
You go into details of duration and displacement , when you really need more kinds of touch events.Best approach depends on scenario. So, if your touchscreen doesn't have a notion of swipe or long press or drag n drop, you might just fire a click on every up event, simplest scenario.
If you absolutely must implement your own, I would use the option 2.
if(motionEvent==MotionEvent.ACTION_UP){
long duration = motionEvent.getDownTime() - .getEventTime();
if(duration < THRESHOULD)
click();
}

Create a key pressed function for button

i want create a key pressed (hold key) func for my application.
for example when I press (hold) a button next year the values of textview grow and when I pickup my finger from button, stop working.
anyone can help me i most use which of Listener or function for create it?
this is my setOnClickListener but i want button press(hold) work
nextDay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (values >= downrange && values <= uprange)
values++;
if (values > uprange)
values = downrange;
if(values <10)
textDayo.setText(PersianReshape.reshape("0"+String.valueOf(values)));
else
textDayo.setText(PersianReshape.reshape(String.valueOf(values)));
}
});
thanks for your help
and sorry for bad english
when I press (hold) a button next year the values of textview grow
Have you looked at the DatePicker? This allows you to set a date very easily:
You can follow this tutorial
If you do not want to use a DatePickker, you can try to use an OnTouchListener and a Handler to do this yourself, but this is hard to do.
I understand that you are trying to hold a button (widget) and while holding it to increase the year.
If this is the case then this link should help you. The idea is that you use a timer to update the ui. Something similar is also described here. If your problem is just setting the date though, a simpler way could be the one suggested by Sam above.

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.

How to count yes and no answers using radio buttons

I don't know if this was asked or not (I searched a little but no result) but I'm kinda time pressed and I need help
I've been developing an application in Android and I've only started under this platform for 3 months
I have a choice test with different questions and yes and no answers using radio buttons
I want to count the "yes" answers but even like this if I put r1.isChecked() instead of for and buttons[i].isChecked() it counts the clicks
Here is what I tried until now and I get force close everytime I click on the first radiobutton RadioButton[]buttons={rb1, rb3,rb5} ;
public void onClick(View view){
checkStates(buttons);
}
private void checkStates(RadioButton[] buttons) {
for (int i=0; i<buttons.length; i++) {
if (buttons[i].isChecked())
da++;}
tv.setText("Result:"+yes);
}
How can I tell my appplication that the radiobuttons are already checked and no need to increment it on a second click (on the same radio button?)
Is there a way to count using the ids or the name of the radiobuttons?
If so how should I do it? Some tutorials (I love them) and tips would help a lot.
Thanks.
You wouldn't want to count each click or even each "state change" because that wouldn't be accurate.
The best thing to do would be to create a function called checkStates(RadioButton[] buttons) { }
You would call this function each time the user clicks a RadioButton.
You would have all your RadioButton objects stored in an array, which you pass to the function, and then the function goes through the list of RadioButtons and checks their state. If it counts 6 as being Yes, then go to your other activity.

Categories

Resources