Application hangs when Timer is turn off/on frequently - android

I have Multi-Shot facility in my camera application. I am using following short of code to do that.
if (TIMER_STARTED) {
multishotTimer.cancel();
multishotTimer.purge();
multishotTimer = null;
TIMER_STARTED = false;
} else {
multishotTimer = new Timer();
multishotTimer.schedule(new TimerTask() {
#Override
public void run() {
TIMER_STARTED = true;
Camera camera = surfaceView.getCamera();
camera.takePicture(null, null,
new HandlePictureStorage());
}
}, 1000, 5000L);
}
Here, TIMER_STARTED is boolean flag which indicate whether timer is started or not. HandlePictureStorage is class which handles PictureCallback.
Question:
When first time i click on "MultiShot" button, it will start timer and take picture every 5 seconds. To stop timer, I one more time click on same button. But if I continuously clicking on button, application hangs and force stopped. Then after I need to switch off my device due to camera is used by stopped service and can't release it lightly. How can I manage start and stop timer?

You don't need TIMER_STARTED to choose whether the button will start or stop the multishot: you can simply check if (multishotTimer != null).
But even after this fix, clicking too fast may be dangerous: you should not create a new Timer between takePicture and HandlePictureStorage.
private bool isCapturing = false;
#Override
public void onClick(View v) {
if (multishotTimer != null) {
multishotTimer.cancel();
multishotTimer.purge();
multishotTimer = null;
}
else if (!isCaptureing) {
multishotTimer = new Timer();
multishotTimer.schedule(new TimerTask() {
#Override
public void run() {
isCapturing = true;
Camera camera = surfaceView.getCamera();
camera.takePicture(null, null,
new HandlePictureStorage());
}
}, 1000, 5000L);
}
}
class HandlePictureStorage implements ... {
#Override
public void onPictureTaken(...) {
isCaptureing = false;
}
}

You need to move TIMER_STARTED = true; from the timer task to the else part, for example after scheduling the timer.
To improve the performance, you should create a TimerTask field instead of recreating an anonymous class every time you create and start the timer.

Related

Issue in creating a full time running Background service in android app

I am creating an android app which needs a background service that fetches location and sends data to firebase every 20 seconds.The service has to start on button click and run continuously even when screen is turned off and should stop again on button click. At first , I tried using alarm Manager but it was not performing tasks at regular intervals. Next I tired using an Async Task and it was invoking a service which was performing task of sending data to firebase. But this approach, did not work on android 8+ versions. Then later on I used the similar approach but with JobIntent service and this approach worked well in android 7(appo) and even in android 8(lava) but in 8+ version(appo reno and mi) maybe due to custom OS , the service does not work if screen is turned off . I tried alternatives like workmanager but it did not work well in higher versions.
I created an activity named punch activity which has two buttons and code is as follows -
This button uses an async activity which calls service every 20 seconds.
#Override
public void onClick(View v) {
if (punchedIn){
Toast.makeText(PunchActivity.this, "Already PunchedIn",
Toast.LENGTH_LONG).show();
}
else {
timertask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
Intent intent = new Intent(PunchActivity.this, BackgroundService.class);
//sendBroadcast(intent);
BackgroundService.enqueueWork(PunchActivity.this, intent);
}
});
}
};
timer = new Timer();
timer.schedule(timertask, 0, 20000);
}
}
}};
This button stops the service
#Override
public void onClick(View v) {
punchedIn = false;
Toast.makeText(getApplicationContext(),"PUNCHED OUT",Toast.LENGTH_SHORT).show();
Log.d("Message","Process "+timer.toString());
if (timer != null) {
Log.d("Message","Process is killed");
timer.cancel();
timer = null;
wakeLock.release();
}
}
});```
The code for JobIntentService is as below
public class BackgroundService extends JobIntentService implements com.google.android.gms.location.LocationListener {
private static Context mContext;
private FusedLocationProviderClient fusedLocationProviderClient;
public static String latitude = "", longitude = "";
public static void enqueueWork(Context context, Intent work) {
mContext = context;
enqueueWork(context, BackgroundService.class, JOB_ID, work);
}
#Override
protected void onHandleWork(#NonNull Intent intent) {
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
//This task does the task of fetching location and sending data to firebase
YourTask();
}
}```
I have made use of power manager in punch activity but it did not work fine. So please give some suggestions or even rectify my approach if you find any issue, based on my use case . Any small clue could be helpful.
Thanks,
Vrashab
Just create a sub thread and request location in a loop like below:
private HandlerThread thread = new HandlerThread("location_thread");
private Handler locationHandler = new Handler(thread.getLoop())
private boolean sholdStop = false
private Runnable locationRunnable = new Runnable() {
while(!sholdStop) {
// location logic
...
Thread.sleep(20000);
}
});
// start to location per 20 seconds
public void startLocation() {
locationHandler.removeCallbacks(locationRunnable);
sholdStop = false;
locationHandler.post(locationRunnable);
}
public void stopLocation() {
sholdStop = true;
locationHandler.removeCallbacks(locationRunnable);
}
But if your app is killed by Android system, this code will be invalid. To solve this problem you might need some method to keep your app lives as long as possible when running background.

How can I reset the time of a postDelayed runnable in onClick?

I've got a listView that gets populated from a server. In the onClick of the ListItem, I display a button for a x number of seconds and I make it invisible again. How can I reset the time every time the onClick is called?
Here is my listItem onClick:
private void displayInCallButton() {
mButton.setEnabled(true);
if (canDisplayInCallControlls) {
canDisplayInCallControlls = false;
fadeInAnimation(mButton);
mButton.setEnabled(true);
mFrontView.postDelayed(new Runnable() {
public void run() {
fadeOutAnimation(mButton);
mButton.setEnabled(false);
hasAnimationEnded = true;
canDisplayInCallControlls = true;
}
}, 5000);
}
}
Thank you in advance.
You have to remove the callbacks and set it once again with the new one with the reset time.
first, set the call back like
Runnable myRunnable = new Runnable() {
#Override
public void run() {
fadeOutAnimation(mButton);
mButton.setEnabled(false);
hasAnimationEnded = true;
canDisplayInCallControlls = true;
}
};
then set it to mFrontView like,
mFrontView.postDelayed(myRunnable,5000)
If you want to reset, do it like
mFrontView.removeCallbacks(myRunnable);
mFrontView.postDelayed(myRunnable, 2000);
How can I reset the time every time the onClick is called?
There is no built-in mechanism to accomplish that.
You can, however, keep a reference to the Runnable you post, remove it and then repost it again to restart at the original delay.
The result would look somewhat like this in its most simple form:
Runnable mDelayedRunnable = new Runnable() {
#Override public void run() {
fadeOutAnimation(mButton);
mButton.setEnabled(false);
hasAnimationEnded = true;
canDisplayInCallControlls = true;
}
};
private void displayInCallButton() {
mButton.setEnabled(true);
if (canDisplayInCallControlls) {
canDisplayInCallControlls = false;
fadeInAnimation(mButton);
mButton.setEnabled(true);
mFrontView.removeCallbacks(mDelayedRunnable);
mFrontView.postDelayed(mDelayedRunnable, 5000);
}
}
You can safely call removeCallbacks() with a Runnable that was never posted in the first place (or even null).
If you don't want to keep an explicit reference to the Runnable, you could also opt to tag the view with it. Just don't forget to clean up on i.e. orientation changes and the like.

How can I control a timer in android?

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.

Reset a timer in android

I have a timer running in my android application. I want that after user interaction the timer should reset itself? is there any function like reset() that resets the timer? What is the procedure to reset the timer?
try this I have used this code every where... when I need to restart timer. working fine for me.
// FOR CANCELING TIMER AND RESTARTING
my_timer.cancel();
if (my_timer == null) {
my_timer = new Timer(true);
}
if (my_timer != null) {
my_timer_task = new TimerTask() {
#Override
public void run() {
// CODE YOU WANT TO EXECUTE WHEN TIMER HITS.
}
};
}
hope it will help you.

A timer which auto-click this button every 20seconds

I have this button which i want to set a timer to it so that the user do not have to click it everytime, such that it auto click this button every 20seconds. How do i set it?
Basically i am using a tabhost activity, so there're total of 3 tabs. In the first tab, there is this button which i need to click the button therefore i then able to retrieve informations from webservice and this webservice will update every time. When i click on other tabs and back to the first tab, i want it to be auto refresh.. Instead of clicking the button to refresh.
holder.btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
A more efficient way IMO is using ScheduledExecutorService:
private void doTheActualJobWhenButtonClicked() {
// put whatever you need to do when button clicked here
... ...
}
... ...
holder.btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// job triggered by user click button:
doTheActualJobWhenButtonClicked();
}
});
... ...
ScheduledExecutorService scheduleTaskExecutor= Executors.newScheduledThreadPool(1);
// This schedule a task to run every 20 seconds:
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
// job triggered automatically every 20 seconds:
doTheActualJobWhenButtonClicked();
}
}, 0, 20, TimeUnit.SECONDS);
UPDATE:
If your button click perform some UI update for example refresh text in a TextView, then simply wrap
your method call within runOnUiThread():
private void doTheActualJobWhenButtonClicked() {
myTextView.setText("refreshed");
}
ScheduledExecutorService scheduleTaskExecutor= Executors.newScheduledThreadPool(1);
// This schedule a task to run every 20 seconds:
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
// involved your call in UI thread:
runOnUiThread(new Runnable() {
public void run() {
doTheActualJobWhenButtonClicked();
}
});
}
}, 0, 20, TimeUnit.SECONDS);
Also you need shutdown ScheduledExecutorService properly before open next Activity or close your current Activity:
// Shut down scheduled task before starting next activity
if (scheduleTaskExecutor != null)
scheduleTaskExecutor.shutdownNow();
Intent intent = new Intent(getBaseContext(), NextActivity.class);
startActivity(intent);
... ...
public void onDestroy() {
super.onDestroy();
// Shut down scheduled task when closing current activity
if (scheduleTaskExecutor != null)
scheduleTaskExecutor.shutdownNow();
}
Hope this help.
Since you have a button, I assume that you have an ActionPerformed-type method at someplace.
Given that, you can do this:
public class AutoClick extends Thread {
// Time to wait in milliseconds
private long wait;
//Latency excepted
private long lat;
AutoClick(long time, long latency) {
wait = time;
lat = latency;
}
public void run() {
long start = System.getCurrentTimeMillis();
long current;
while(true)
current = System.getCurrentTimeMillis();
long step = (current-start) % 20000;
if(step <= latency || step >= wait-latency)
//call the action-performed method
}
}
Then create an instance of the thread and run it:
public AutoClick clicker = new AutoClick(20000);
clicker.run();

Categories

Resources