I would like to hide the zoom controls on a timed interval. Is there something I can call in the API to set the controls to fade out like they did in v1? Currently I've written some code to hide the controls after a set interval but before I go any further with it I was curious if there was already a known and better way of going about this.
mTimer = new Timer();
mapFragment.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
mapFragment.getUiSettings().setZoomControlsEnabled(true);
hideCameraOnInterval(5000);
}
});
And this is the method hideControlsOnInterval
public void hideCameraOnInterval(long milliseconds){
TimerTask tt = new TimerTask() {
#Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
mapFragment.getUiSettings().setZoomControlsEnabled(false);
}
});
}
};
mTimer.schedule(tt, milliseconds);
}
If there is not a better solution than I will implement logic to restart the TimerTask if it is going to be scheduled again before the delay is reached. If anyone has either a better solution or a good way to restart the task please submit an answer.
Related
I am using animation library in my app(For scanning).Whenever device goes to batter saver mode animations are being disabled.In some other apps animations are not disabled. Is there any work around to enable animations in batter saver mode ? If there is no work around then How other apps are achieving it ?
At last I achieved it through Count down timer.For every interval I am changing properties of view like alpha,ScaleX & ScaleY. And if I want to repeat that animation I wrapped it in a handler. I dont know whether it is best approach or not. I am posting here so that it might help someone.
Handler mHandler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
View view = new View(context); // View to animate
view.setScaleX(0);
view.setScaleY(0);
view.setAlpha(1);
CountdownTimer countDownTimer = new CountDownTimer(animationDuration, 1) {
#Override
public void onTick(long l) {
float value = l / 2000f;
view.setScaleX(1 - value);
view.setScaleY(1 - value);
view.setAlpha(value);
}
#Override
public void onFinish() {
removeView(view);
handler.postDelayed(runnable, 500);
}
};
countDownTimer.start();
}
};
handler.postDelayed(runnable, 500);
I want to make an application about mini game.
Detail : In 2 seconds you must to answer a question if you don't answer or the answer is wrong -> Game Over . But if your answer is true the Timer will reset become 0 and countdown again with diffirent question.
I have already seen many code about timer in website but I don't understand clearly about it :(
So I want to ask : How can i set up a timer run only 2 seconds and how can i reset it and continue with a new question ?
Please help me.
you can use CountDownTimer in android like this:
public class Myclass {
myTimer timer =new myTimer(2000,1000);
public void creatQuestion(){
timer.start();
//method you init question and show it to user
}
public void getUserAnswer(/*evry thing you expected*/)
{
//if answer is true call timer.start()
//else call timer.onFinish(); to run onfinish in timer
}
public class myTimer extends CountDownTimer {
public myTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
// you can update ui here
}
#Override
public void onFinish() {
this.cancel();
//fire game over event
}
}
}
i hope it make you satisfy
I've done something similar using Thread/Runnable.
Thread t = new Thread(new Runnable() {
public void run() {
final long startTime = getTime();
final long maxEndTime = startTime + 2000L;
try {
while (shouldContinueWaiting()) {
if (getTime() > maxEndTime) {
throw new TimeoutException();
}
sleep();
}
} catch (InterruptedException e) {
handleInterrupt();
} catch (TimeoutException e) {
handleTimeout();
}
}
boolean shouldContinueWaiting() {
// Has the user already answered?
}
void handleInterrupt() {
// The user has answered. Dispose of this thread.
}
void handleTimeout() {
// User didn't answer in time
}
void sleep() throws InterruptedException {
Thread.sleep(SLEEP_DURATION_IN_MILLIS);
}
void getTime() {
return System.currentTimeMillis();
}
then you can start/restart the thread by:
t = new Thread(same as above...);
t.start();
and stop by:
t.interrupt();
We want to use the Timer class.
private Timer timer;
When you're ready for the timer to start counting -- let's say it's after you press a certain button -- do this to start it:
timer = new Timer();
timer.scheduleAtFixedRate(incrementTime(), 0, 100);
The first line is us creating a new Timer. Pretty standard. The second line, however, is the one I wanted you to see.
incrementTime() is a method that is called at the end of every "tick" of the clock. This method can be called whatever you want, but it has to return an instance of TimerTask. You could even make an anonymous interface if you want, but I prefer moving it off into its own section of code.
The 0 is our starting location. We start counting from here. Simple.
The 100 is how large a "tick" of the clock is (in milliseconds). Here, it's every 100 milliseconds, or every 1/10 of a second. I used this value at the time of writing this code because I was making a stopwatch application and I wanted my clock to change every 0.1 seconds.
As for your project, I'd suggest making the timer's task be your question switch method. Make it happen every 2000 milliseconds, or 2 seconds.
You can use a Handler.
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
//this will happen after 2000 ms
}
}, 2000);
Maybe this can help you:
Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
// FIRE GAME OVER
handler.postDelayed(this, 2000); // set time here to refresh textView
}
});
You can fire your game over after 2000 milliseconds.
If you get the question correct -> remove callback from handler and reset it when the next question starts.
Hey guys
I'm trying to develop a little slide show in android. I've implemented the timer with a timertask and as expected the "run"-method is called everytime after the delay. But, however, when the delaytime has passed, the run method is called twice, so that the slide show slides two pictures instead of one.
Can anyone help me to solve this problem? Heres some code for better understandig what i've done.
public void startTimer() {
if(timer == null) {
timer = new Timer();
}
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
timerHandler.post(new Runnable() {
#Override
public void run() {
Log.d("Hier herein", "Timer abgelaufen");
}
});
}
}, 7000, 7000);
}
The startTimer()-Method is only called once, so I really don't understand, why the function is called twice.
Thanks in advance :D
I know how to dim status bar on Android tablets. I do this with that code:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
It works fine but only once. When I touch status bar, it activates and when after that I'm back in my application's activity, status bar is still activated (with icons instead of dots). I tried to log onResume calls, but it's not called, so I googled again and found another solution - using handler for changing visibility of status bar:
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
});
It sometimes works but after several tries it breaks down.
I need this for game - theoretically I can try to call it after every touch or every time in main loop but this seems to be bad idea (and additionally it must be called from specific thread - Only the original thread that created a view hierarchy can touch its views.).
My question is: what is the best way to implement auto dim of status bar? Or, in my situation, any way.
A solution to this is having a View.OnSystemUiVisibilityChangeListener on you root view.
When an event fires you have to wait some time to dim them again. A Timer would be suitable.
private class MySystemUiVisibilityChangeListener implements View.OnSystemUiVisibilityChangeListener {
#Override
public void onSystemUiVisibilityChange(int visibility) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
MyActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
mRootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
});
}
}, 1000);
}
}
I've been running into this issue as well (but for the hide navigation flag) and it just seems like google natively disabled the ability to constantly hide/dim the system bar. I do agree with you that constantly calling that method might fail on threads or cause performance issues especially given the fact that so many ontouch events occur on the mobile device.
ultimately, I don't think there's a solution to this unless the user rooted their device to permanently remove the system bar.
I altered Karanlos answer a little bit:
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
AudioRecordTest.this.runOnUiThread(new Runnable() {
#Override
public void run() {
if (ivIndicator.getVisibility() == View.VISIBLE)
ivIndicator.setVisibility(View.INVISIBLE);
else if (ivIndicator.getVisibility() == View.INVISIBLE)
ivIndicator.setVisibility(View.VISIBLE);
}
});
}
};
timer.scheduleAtFixedRate(task, 0, 500);
you don't need to make a new class or implement anything. just make a TimerTask as in his answer and schedule it using scheduleAtFixedRate(task, delay, period), this method will run the TimerTask every period of time in milliseconds.
Not sure if this was available back in 2012, but you can now use the activity's onWindowFocusChanged() callback:
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE
}
I'm extending the CountDownTimer class to obtain some custom functionality .In onTick() in case some conditions are met I call cancel() , expecting that will be the end of it, however the onTick() callback gets call until the the count down is reached . So how to prevent this from happening ?
CountDownTimer.cancel() method seems to be not working. Here's another thread without a solution Timer does not stop in android.
I would recommend you to use Timer instead. It's much more flexible and can be cancelled at any time. It may be something like that:
public class MainActivity extends Activity {
TextView mTextField;
long elapsed;
final static long INTERVAL=1000;
final static long TIMEOUT=5000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextField=(TextView)findViewById(R.id.textview1);
TimerTask task=new TimerTask(){
#Override
public void run() {
elapsed+=INTERVAL;
if(elapsed>=TIMEOUT){
this.cancel();
displayText("finished");
return;
}
//if(some other conditions)
// this.cancel();
displayText("seconds elapsed: " + elapsed / 1000);
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, INTERVAL, INTERVAL);
}
private void displayText(final String text){
this.runOnUiThread(new Runnable(){
#Override
public void run() {
mTextField.setText(text);
}});
}
}
CountDownTimer is also working fine for me, but I think it only works if you call it OUTSIDE of the CountDownTimer implemetation (that is don't call it in the onTick).
Calling it inside also didn't worked.
I tried this code snippet, since most answers are saying you cannot cancel the timer inside its implementation, thus i tried using a handler inside onFinish. Old post but if anyone comes across this its helpful.
new Handler().post(new Runnable() {
#Override
public void run() {
timerTextView.setText("00:" + String.format("%02d", counter));
cancel();
}
});