I want to send data to an arduino mega 2560 as long as a button is being pressed and when that button is released it will stop sending informations. I am using onTouchListener with MotionEvent constants. But when I run this on my phone I press the button and it sends data even though after a while I release it. Where am I being wrong here?
switch (v.getId()) {
case R.id.left1: // check what button is pressed
while(event.getAction() == MotionEvent.ACTION_DOWN) {
bt.sendData("1"); // while pressing the button it sends data
}
if(event.getAction() == MotionEvent.ACTION_UP) {
// when it stops, do nothing
}
break;
}
return true;
Your problem is in infinite loop while(event.getAction() == MotionEvent.ACTION_DOWN) that you start upon receiving the first event.
OnTouchListener is called for each event that is dispatched to view, down and up are separate events and event does not change while being processed.
So to solve your problem - you need to send data from a separate thread.
Start it on ACTION_DOWN and also have a flag that will be modified on ACTION_UP to indicate thread to exit.
You have to set the flag of bt.sendData to false when button is released which seems to be absent in your code.
It's like you open tap for water but forget to close the tap when you are finished. Hope it helps
Related
I have a view (WebView to be specific). In that view, I have something like:
setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//user has clicked
}
}
});
will this 100%, ALWAYS guarantee me that the user has tapped/clicked on the view? If not, under what cases would this not guarantee me a click??
I want to intercept all user "clicks". think of "clicking" like you would "click" a button, but just on a mobile device. Imagine this code being called 100 million times by different devices
MotionEvent.ACTION_UP is when you lift up your finger from the screen. You can be sure the user has touches a view once the onTouch is called.
I think that while MotionEvent.ACTION_UP won't always be called when the touch ends (as mentioned here: ACTION_UP not always called?), it's certainly safe to assume that a user has touched the screen. That's how the API describes it, IMO:
A pressed gesture has finished, the motion contains the final release
location as well as any intermediate points since the last down or
move event.
I wanted to get Get Key press, Key release Events out of my Activity Screen
i.e ex When i am in Android Native Home Screen and if i Press Search KEY i need to fire my API..Is it Possible??
When i am in Android Native Home Screen and if i Press Search KEY i need to fire my API..Is it Possible??
No. Key events are generally only delivered to the foreground activity. The two exceptions are the CAMERA button (on those few devices that have one) and the MEDIA button (found on headsets) -- those are sent as broadcast Intents if the foreground activity does not consume the event.
I don't know about the SEARCH button, but BACK button you can override.
As I know HOME button can't be override.
Maybe consider using MENU button to display option menu?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//your method
return false;
}
return false;
}
I'm starting an activity from a service, which displays some text.
Now if a user presses a key - say a back key, I would like that back button to be processed by any other app which may be running (not started by my service).
Is this possible? I have tried FLAG_NOT_FOCUSABLE, however, the back key is still not processed by any other app/ignored. Android, after some time keep giving a option for "Force Close" or 'Wait".
Any pointers will be helpful.
Just FYI, "whitepages" app does something like this - it shows a dialog window, however, doesn't process the back key.
Thanks.
I'm not sure if this is correct or not, but I've seen the same behavior in a few apps myself. I think they are using Toast messages to display the text. I'm not sure if a service can display Toast or not - you'll have to try... if not then you could also try having the Activity display the toast and immediately exit.
Only the currently running activity receives in the button pressed signal. All you can do is intercept the back button press by implementing:
onBackPressed()
On older devices this won't quite work and you'll need to do something like:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
As far as passing the button press to another activity, I'm not sure you can do that. What you could do is pass the activity an intent that could specify the back button was pressed. However, this assumes that the other activity is setup to deal with such an intent.
i've been searching the web but i find no answer to my question. I have a button, when you press it, it will play a sound. The problem is that when you touch a button on the screen it goes to the onClickListener() only after the button have been released. I need it to run the listener when the button is pressed not when it's released, because this cause a delay when playing the sound. I tried onTouchListener() and it didn't work either, because the sound get's played every time i move the finger over the button. I tried onKeyDown() but it won't work for screen buttons. Any ideas? Some help will be appreciated.
Thanks
You can use OnTouchListener and test the event action:
public boolean onTouch (View v, MotionEvent event) {
if (event.getAction () == MotionEvent.ACTION_DOWN) { // ...
}
}
Maybe you can still use your implementation for onTouchListener(). Just set a boolean flag for when the user starts touching the button, then while it is set true (meaning the user hasn't released the button yet) do not play the sound. When the user releases the button (ACTION_UP), set the flag back to false. This would mean you are ready to play the sound again.
I want to get continues reading from a continues touch down event.
I mean that I want to get event every 0.01 sec.
I've tried to use GestureDetector and I got down, show and long events during a continues
touch. After the long event I don't get any events.
Please help...
You need to listen for TouchMotion events in the Activity's onTouch() method.
Where are you logging the events? The onTouch() handler should be called whenever the user touches the screen. You can do a switch case on the event to determine what the user is doing:
switch(event) {
case MotionEvent.ACTION_DOWN:
// When user first touches screen
break;
case MotionEvent.ACTION_MOVE:
// When user is dragging, or continuous touch
break;
case MotionEvent.ACTION_UP:
// When user stops touching
break;
}