How to make a protection of many times pressing the button? - java

In my app I use a camera, and I want to take a pictures. In my app is button (Photo). If I press it one times - all work perfect, but if I press button many times until camera take picture, my app hangs. How can I fix it?

In your onClickListener call Button.setEnabled() and set it to false.
Then set it to true when you've finished taking the photo.

Use a listener that disables the button on press (setEnalbed(false)), than start a countdown thread that re-enables it after some time, 200ms maybe, or whatever fits the best.
After second thoughts this might not be a really good idea.
There is a chance that the thread will not be scheduled to run, so if you exactly know the point when you can re-enable the button in your code, don't use threads.

Related

Enable/Disable Touch Screen on Android

Is there any way of disabling / enabling the touch events in a certain activity
without disabling certain buttons. like if the phone is "frozen" on a certain activity page for limited time?
I need that the user wont be able to go back on page, slide out or any other events.
Is it possible? (I'm using android studio, java)
Thanks in advance!
Any answer would help :)
It's possible.
Actually there is no special function to make it.
To make it, you can make a full screen button with transparent background.
And you will define a function of the button.
Of course, it will be an empty function.
Well, you can do nothing on the screen.
and then you can hide or visible the button according to your necessary.

Set a timer for the visibility of a button

This is purely theoretical put let's say once I put a timer for a button to appear only 84600 second later (like 24h), will it the count down continue after the app is shut down ?
Can the user close the app and then come back the next day and find the button after the countdown is done ?
Or is there a better way to make it happen ? Like to allow the user to see it only after 24h ?
you can use ScheduledExecutorService to create such a timer. Then on every tick, you can open the app if you want, and show the button, or just show the button if the user open the app.
documentation is available here.

Run program in background after the click of a button

I want to make my program run in the background after you click a button.
I'm creating a rickroll (in 2015 yeh) and when you click a button I want to play the song never gonna give you up from rick astley and get the frame closed and run in a process, so the song still continues. I already got the part of clicking the button and starting the song, but how can I make it continue in a process?
I do not think that "continue in a process" is what you want.
Just close your frame, allow your main message loop to end, and before returning from your main() function block waiting for the sound to end. Or, if you don't know how to block waiting for the sound to end, then calculate how many milliseconds long your sound is, and just Thread.sleep() for that many milliseconds.

Android: how to stop listener when app closes

When my app starts I start a location listener, I'd like to turn it off when the user exits the app, whether by pressing the back button or just putting it in the background, and I would like to resume listening when the app is back.
I couldn't find onPause/onResume for the app level, so I thought about holding a counter that will count the number of active activities and if it's 0 it will shut down the listener, but there must be a better way...
Please advise.
To detect if your app is running in background you can read this article:
http://www.vardhan-ds.co.in/2013/05/android-solution-to-detect-when-android.html
And to shut down your listener, you just need to remove the Listener from your manager.
If you need some help with this I can post some code, but I think this description will help you.

Random RSS Data

I'm kind of new to android so please bear with me.
I need help with something, I have a rss feed that has energy tips in it. Instead of displaying all of them inside a listview I would like to display them one at a time on an activity that has a timer that automatically switches them inside a textview this activity would also have two buttons "prev" and "next" which would override the timer. Is it possible? Any pointers would be appreciated.
Thanks,
It sounds like the kind of view you might be looking for is a ViewSwitcher. This will allow you to switch and animate between different views in your app. While you can imagine more elaborate configurations (really, anything), this might be a good choice to you that's already baked into the framework. How to control the view is a slightly different question, however, there are multiple ViewSwitcher tutorials out there, and you can easily implement a Timer which switches between the views. What you do is fairly simple: tell the timer to fire after some interval. If the user presses some button before the timer fires, cancel the timer and (perhaps) reset it?

Categories