I am using a Canvas in a SurfaceView Class which runs on a thread. The SurfaceView is called by an Activity (setContentView(surfaceview)). When the screen turns off, it goes through a pause() method which turns off all loop variables and so on.
The strange thing is: while on my tablet (Android 4.4.2) the Thread pauses and resumes correctly (the thread starts all over), however on my phone (Android 4.2.2) and on other people's phone (CM 11) the app hangs (it doesn't crash! it just hangs) and I have to kill it. LogCat doesn't print anything - however I tried to find out what happens using Log.i, it seems that after the pause method finishes, the pause method starts again, and this is where the app hangs.
I have checked before and the pause/resume methods are getting called correctly (override of onPause/onResume of the Activity using super.onPause()/Resume() and then surfaceview.pause()/resume())
My pause/resume methods:
public void resume() {
countdown = true; // this one,
threadRun = true; // this one and
finishWait = true; // this one each are loop controllers
won = false;
hitCount = 0;
thread = new Thread(this);
thread.start();
}
public void pause() {
Log.i("codeflow", "Pause method start");
// all loop controllers have been set to false
countdown = false;
threadRun = false;
finishWait = false;
/* Log.i("CodeFlow", "drawablank");
drawABlankScreen(); // to hide everything drawn */
while (true) {
try {
Log.i("codeflow", "Thread.join() loop");
thread.join();
break;
} catch(InterruptedException e) {
Log.i("codeflow", "InterruptedException");
e.printStackTrace();
}
}
thread = null;
Log.i("codeflow", "Thread is killed");
if (won && !lost) { // !lost IS necessary
Log.i("CodeFlow", "save highscore");
saveHighscore(highscore);
}
}
The pause method goes through everything the way it should, but then it starts again (another "Pause method start") and goes either to drawABlankScreen() or, when this linke is commented, it prints "Thread.join() loop" once and then the app hangs.
The thing that bugs me the most it that it hangs on my phone while it doesn't hang on my tablet.
Can anyone please help me, I've searched for hours and found nothing....
Joining the UI thread to a background thread in onPause() is probably what's causing your problem.
Instead, you should kill the thread.
"thread.join()" will hangs the thread which execute this sentence, until thread run over. Strangly, if execute in Android UI thread, Android would not crash.(sorry for my full of mistakes English).
Related
I made clone of ""Flappy bird" game by watching video tutorials.i programmed it so that when the bird falls or collides with the tubes a game over message appears on the screen and the game restarts when the player taps on the screen.
The problem is that when the user fails to tap the bird in time and it collides with the tube,the game over screen appears immediately and the user happens to tap on the game over screen which results in restarting of the game.
This makes the user unable to see the score.I have already tried using Thread.sleep().Following is the code
(gameState == 2)
{
batch.draw(gameOver,Gdx.graphics.getWidth()/2-gameOver.getWidth()/2,Gdx.graphics.getHeight()/2-gameOver.getHeight()/2);
try
{
Thread.sleep(2000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
if (Gdx.input.justTouched()) {
gameState = 1;
startGame();
score =0;
scoringTube = 0;
velocity = 0;
}
}
With this code the problem is that even the gameover image is being delayed and the previous problem is still occuring but now with a delay.I basically need a way so that justTouched method becomes inactive for a while when the game over screen is there.Please help.
I really wouldn't recommend using Thread.sleep; instead, you could try to use a boolean that is changed once the game ended, and prevent the method from executing in that case. Combine that with e.g a Timer that resets it after a fixed delay, and you should have the solution to your problem.
Example Usage for the timer:
new java.util.Timer().schedule(
new java.util.TimerTask() {
#Override
public void run() {
//execute code here (change boolean state)
}
},
yourDelayHere
);
I'm writing an android app which should take pictures in a user-defined interval (20 sec - 1 min). It should take the pictures even while it is running in background or while the device is sleeping. The app will run for a very long time period. If it is necessary to wake up the device, it should put back to sleep as soon as possible to save batterie life. After taking a picture the app will process some additional work (comparison of two pictures).
I read some stuff about sheduling alarms (http://developer.android.com/training/scheduling/alarms.htm), creating Services (also # android training) and Android AsyncTasks, Java threads (http://www.mergeconflict.net/2012/05/java-threads-vs-android-asynctask-which.html)
... but I'm still not sure what is the best way to achieve this.
My questions are:
Should I use thread or a task to take the pictures in background?
(the comparison of the two pictures might take longer than a few
milliseconds but i don't know anything about the cpu load of this
operation)
Should I use an alarm to wake the device up or are there any alternative solutions?
How can both (alarms and thread/task) work together? (Include the Alarm in the Task/Thread?)
Many thanks for your help in advance.
As to our question I know I can help get started with the aspect of repeating the picture taking task at a user defined time interval. For such a task you can user a Timer to achieve this. The code would look something like this:
mTmr = new Timer();
mTsk = new TimerTask() {
#Override
public void run() {
//Take picture or do whatever you want
}
};
mTmr.schedule(mTsk, 0, USER_DEFINED_EXECUTION_INTERVAL);
schedule begins the timer. The first parameter of schedule used here is the task to run which is mTsk. The second parameter is the delay until the first execution (in milliseconds), in this case no delay. The third parameter is what you'll want to manipulate which is the interval of executions. The parameter is the time between executions so if it were 20 seconds you'd pass in 20,000. If it were a minute it would be 60,000. You can get this value from the user using any method you'd like.
To keep the timer running make sure you don't call mTmr.cancel in onPause because for your case you want to keep the timer running while the user isn't on the app. Not calling cancel means the timer will hold it's resources until the app is closed by the user.
OR you can look at this How to schedule a periodic task in Java? If you'd like to use ScheduledExecutorService instead of a Timer.
I have made this app - Lenx. It uses Camera extensively and I am processing image in the background. I have used AsyncTask to process the image and it has never given any problems. The app also has a timer which starts the process after certain interval. The logic that I have used is very simple.
I have not used Camera2 API yet, so the code might be deprecated. I created CameraPreview class which implements Camera.PreivewCallback.
#Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (data == null) {
return;
}
int expectedBytes = previewWidth * previewHeight *
ImageFormat.getBitsPerPixel(ImageFormat.NV21) / 8;
if (expectedBytes != data.length) {
Log.e(TAG, "Mismatched size of buffer! Expected ");
mState = STATE_NO_CALLBACKS;
mCamera.setPreviewCallbackWithBuffer(null);
return;
}
if (mProcessInProgress || mState == STATE_PROCESS_IN_PROGRESS) {
mCamera.addCallbackBuffer(data);
return;
}
if (mState == STATE_PROCESS) {
mProcessInProgress = true;
processDataTask = new ProcessDataTask();
processDataTask.execute(data);
}
}
public void startProcessing() {
mState = STATE_PROCESS;
}
And my AsyncTask is something like this
private class ProcessDataTask
extends
AsyncTask<byte[], Void, Boolean> {
#Override
protected Boolean doInBackground(byte[]... datas) {
mState = STATE_PROCESS_IN_PROGRESS;
Log.i(TAG, "background process started");
byte[] data = datas[0];
long t1 = java.lang.System.currentTimeMillis();
// process your data
long t2 = java.lang.System.currentTimeMillis();
Log.i(TAG, "processing time = " + String.valueOf(t2 - t1));
mCamera.addCallbackBuffer(data);
mProcessInProgress = false;
return true;
}
#Override
protected void onPostExecute(Boolean result) {
mState = STATE_PROCESS_WAIT;
}
}
onPreviewFrame() will always get called as long as the camera preview is running. You need to take the data and process it only when you trigger something. So simply change the state of a variable, in this case, mState, and based on the state, call your AsyncTask.
Edit, found a work around, code is posted at the bottom.
Is it possible to pause a thread that's in the middle of using thread.sleep(sleeptime)? And then when it's resumed, sleep for the remaining time?
I tried implementing the wait/notify set up here to pause a thread
How to pause/resume thread in Android?
However, it looks like it waits until thread.sleep is finished with it's sleeping time.
The reason why I am asking this because I am using a while loop inside a thread to play music from a play list, and I need the thread to sleep for the duration of the music track so that the music track finishes before the while loop plays the next song on the playlist.
For example, the while loop will play a song that has a duration of 50 seconds, so the thread.sleep method will pause the thread that contains the while loop. But, the user wants to the pause the song, and the whole play list 25 seconds into the song. So the user will press the pause button 25 seconds into thread.sleep's 50 second sleep. After the user unpauses, I would like the thread.sleep to continue the rest of 50 second wait (25 seconds left at this point) to complete playing the rest of the song. Then the while loop will play the next track on the playlist.
Runnable whilePlay2 = new Runnable() {
#Override
public void run(){
while (k <= myIntArray2.size()-1 ) {
System.gc();
if( myIntArray2.get(k)>count){
video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
videocursor.moveToPosition(myIntArray2.get(k)-count);
filename2 = videocursor.getString(video_column_index);
}
else{
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(myIntArray2.get(k));
filename2 = musiccursor.getString(music_column_index);
}
try {
if (m2MediaPlayer.isPlaying()) {
m2MediaPlayer.reset();
}
m2MediaPlayer.setDataSource(filename2);
m2MediaPlayer.prepare();
m2MediaPlayer.start();
Thread.sleep(m2MediaPlayer.getDuration());
synchronized(Lock){
while(onS){
Log.i("should be", "waiting");
Lock.wait();
}
}
Log.i("what is on", "on is"+onS);
Log.i("k Check", "k is" +k);
} catch (Exception e) {
}
m2MediaPlayer.stop();
m2MediaPlayer.reset();
k++;
}
} //closes public void run(){
// TODO Auto-generated method stub
};
Thread whilePlay2T = new Thread(whilePlay2);
whilePlay2T.start();
And here is my pause button
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
mMediaPlayer.pause();
length=mMediaPlayer.getCurrentPosition();
m2MediaPlayer.pause();
length2=m2MediaPlayer.getCurrentPosition();
synchronized (Lock){
onS=true;
}
} else {
mMediaPlayer.seekTo(length);
mMediaPlayer.start();
m2MediaPlayer.seekTo(length2);
m2MediaPlayer.start();
synchronized (Lock){
onS=false;
Lock.notifyAll();
}
}
}
EDIT
Looks like I can't use it. So now I am using a recursive function instead of a while loop to set up my play list. Here's what one of them looks like.
public void hiplaylist2(int t2) {
k=t2;
if(k <= myIntArray2.size()-1 ){
System.gc();
if( myIntArray2.get(k)>count){
video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
videocursor.moveToPosition(myIntArray2.get(k)-count);
filename2 = videocursor.getString(video_column_index);
}
else{
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(myIntArray2.get(k));
filename2 = musiccursor.getString(music_column_index);
}
try {
if (m2MediaPlayer.isPlaying()) {
m2MediaPlayer.reset();
}
m2MediaPlayer.setDataSource(filename2);
m2MediaPlayer.prepare();
m2MediaPlayer.start();
//Thread.sleep(mMediaPlayer.getDuration());
//SystemClock.sleep(mMediaPlayer.getDuration());
Log.i("k Check", "k is" + k);
} catch (Exception e) {
}
}
m2MediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer m2MediaPlayer){
m2MediaPlayer.stop();
m2MediaPlayer.reset();
if(k < myIntArray2.size()-1 ){
k++;
hiplaylist2(k);
}
}
});
};
And then I ues a thread to have more than one of these run at the same time.
I don't really get it. I'm not sure about what you want to do, but i guess that there are at least two problems into your algorithm.
Firstly, when you work on concurrent tasks take care of your race conditions, your m2MediaPlayer seems to be one of them.
Concerning your Thread.sleep(m2MediaPlayer.getDuration()); I believe that you shouldn't do it this way, if you want to wait until your song is finished you just have to :
Launch your song from Thread 1
Pause your Thread 1 (wait)
The song is playing in Thread 2
When the song is finished wake up (notify) your Thread 1 from Thread 2 (and let T2 die)
Note that you have to keep your code as simple as you can, because concurrency is hell.
Is it possible to pause a thread that's in the middle of using thread.sleep(sleeptime)? And then when it's resumed, sleep for the remaining time?
Assuming that you are asking about the behaviour of the Java runtime libraries, then the answer is No.
There is no safe way to pause and resume a thread. The Thread suspend / resume methods for doing this are unsafe and were deprecated in Java 1.1. (Read this if you want an explanation.)
Even if you use the unsafe methods, they do not magically pause a sleep timer. In fact, it is not clear what would happen to a sleeping thread: it is not specified.
Indeed, I would not totally be surprised if you found that a sleeping thread whose sleep time expired while suspended never woke up ...
I'll leave it to others to deal with the rest of your question.
(I can't figure out from your description what it is you are trying to do here ... and why you even need to pause a sleeping thread. On the face of it, it sounds like you should be using wait / notify or a higher level synchronization class ... rather than sleeping. The fact that you apparently tried and failed does not make that the wrong solution.)
Here is my loop code (This is the only code relating to my loop):
while(true)
{
try {
Thread.sleep(20);
System.out.println("1");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I launch the applet, it white screens and I cannot close it unless I press the "Terminate" button in eclipse.
You're blocking the UI Thread with an infinite while loop. You don't say whether you're using an AWT or Swing applet, either way the result will be the same. If you're using a Swing applet, use a Swing Timer. If you're using the old heavyweight AWT, convert it to Swing and follow the previous advice.
As said you made an infinity loop with:
while(true){
//something
}
There is no break; So why or what should stop the loop except of a thrown exception?
To see when a InterruptedException is thrown you should read the JavaDoc:
http://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.html
You are hogging the applets EDT. You need to run your loop in another thread. Try adding Thread gameThread; as a variable and use
gameThread = new Thread() {
public void run() {
while (condition) {
//code here
}
}
}
and then gameThread.start() both in your applet start method and gameThread.join() in your applet stop method.
I am trying to stop a for loop that is initiated by pressing a button. The only problem I have found is that the application is literally non-responsive once the start button is pressed. How would I go about making the stop button? At the moment the only way I can stop the application outside of my IDE is to go into task manager and forcibly delete it.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String L = "Hello";
int Num = Integer.parseInt(m1.getText());
int Num2 = Integer.parseInt(m2.getText());
nu = Num;
for (int kk = nu; nu > 0; nu--) {
if (O1.isSelected()) {
for (int num3 = nu; nu > 0; nu--) {
try {
try {
Thread.sleep(Num2 * 1000);
} catch (InterruptedException ex) {}
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_F);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
}
This is the code I have, I have been looking around and I think I need to use the SwingWorker class. I am not sure how to implement it though.
Have you put your loop inside the button action handler?
If so remove it and put it in a thread. Else it will block the EDT and app will become unresponsive. Use buttons only to trigger start and stop.
Without code it is hard to determine, but you are probably performing the loop on AWT thread, hence your UI is blocked and you can not press a button.
You should move your endless loop to another Thread, and then the button will work
You are entering in an infinite loop any how. I would suggest to put your loop in a separate thread and when you want to stop it just interrupt that thread on button click.
Move the code into a dedicated thread and use a variable check to end the loop externally.
The first problem is that you're performing this loop inside the Event Dispatch Thread (EDT). This is a thread which is dedicated to handling the user interface. Since you are looping and sleeping in this thread, it is unavailable to handle any other user interactions. So effectively you are locking yourself out of your own program.
The proper way to shut this down is to create a shutdown() method on the loop which will modify a variable that is checked in each iteration of the loop. Then, when you want to shut it down, you just call this method on the Runnable and the loop shuts down.
public class KeyPressRunnable implements Runnable {
private boolean isRunning = true;
public void shutdown() {
this.isRunning = false;
}
#Override
public void run() {
// put your loop in here, and include this code in the loop:
if (!this.isRunning) break;
}
}