I am trying to get a simple Runnable to execute some code every few seconds, but although I can get it to execute, I cant get it to stop. The code below shows 2 calls startDbChecking() and stopDbChecking(), I have just placed them in the code block to show what I'm attempting - not how the code is set up.
public class MainActivity extends TabActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startDbChecking(); // will run this no problem
stopDbChecking(); // but will not stop
}
public void startDbChecking() {
handler.post(runnableCode);
}
public void stopDbChecking() {
handler.removeCallbacks(runnableCode);
}
private Runnable runnableCode = new Runnable() {
#Override
public void run() {
// Do something here on the main thread
System.out.println("OK");
handler.postDelayed(runnableCode, 2000);
}
};
}
Try this in your Activity: to stop the runnable
protected void onStop() {
super.onStop();
handler.removeCallbacks(runnableCode);
}
Related
I've created a splash screen in my app that displays for 3 seconds before moving to LogInActivity. It looks like this:
public class SplashScreenActivity extends AppCompatActivity {
// Splash screen displays for 3 seconds.
private static int SplashTimer = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
finishTiming();
}
public void finishTiming() {
Handler h = new Handler(Looper.getMainLooper());
h.postDelayed(new Runnable() {
#Override
public void run() {
// App then transitions to login activity.
Intent intent = new Intent(SplashScreenActivity.this, LogInActivity.class);
startActivity(intent);
finish();
}
}, SplashTimer);
}
}
Now I'm trying to test it and I have two tests. testTimer() checks that the timer works as it should and SplashScreenToLogin() checks that the activity moves to the LogInActivity next:
#RunWith(AndroidJUnit4.class)
public class SplashScreenActivityInstrumentationTest {
SplashScreenActivity splashScreenActivity;
private Instrumentation.ActivityMonitor activityMonitor;
#Before
public void createSplashScreenActivity() {
splashScreenActivity = new SplashScreenActivity();
activityMonitor = InstrumentationRegistry.getInstrumentation().addMonitor(LogInActivity.class.getName(), null, false);
}
#Test
public void testTimer() {
assertFalse(splashScreenActivity.isFinishing());
splashScreenActivity.finishTiming();
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
assertTrue(splashScreenActivity.isFinishing());
}
#Test
public void SplashScreenToLogin() {
splashScreenActivity.finishTiming();
splashScreenActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
}
});
LogInActivity logInActivity = (LogInActivity) InstrumentationRegistry.getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 10000);
assertNotNull(logInActivity);
logInActivity.finish();
}
}
My issues is that when I run this I get the following error: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
This error displays at the first line of each test function and I'm not sure why. Can someone explain why it's happening and a potential solution?
I am quite newbie with android development and I am trying to figure out why this does not work.
I run this idea on Eclipse and it works fine. But I cant make it work on any of my devices. The app shows the value at start but it dont refresh the value anymore.
public class MainActivity extends AppCompatActivity {
private TextView txtCrono;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.txtCrono = (TextView) findViewById(R.id.txtTiempo); //activity_main TextView
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
exec.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
txtCrono.setText(String.valueOf(System.currentTimeMillis()));
}
}, 0, 100, TimeUnit.MILLISECONDS);
}
}
Try to use runOnUIThread:
exec.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
txtCrono.setText(String.valueOf(System.currentTimeMillis()));
}
});
}
}, 0, 100, TimeUnit.MILLISECONDS);
Basically, you must access a View from the main UI thread. (I don't know why your code doesn't cause an error and the first runnable is successfully setText. Maybe only the first runnable is executed on the main UI thread.)
I have a .lrc file and I need to go over every line with a CountDownTimer. I have tried using AsyncTask to do so but I get the error:
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
On line new CountDownTimer... I tried doing it with runnable but I still get the same error. My goal is to get it to go over every line in .lrc file which looks like this:
[00:04.15]Help me, it's like the walls are caving in
[00:10.46]Sometimes I feel like giving up
[00:13.63]But I just can't
...
I am not sure how efficient it is to do it the way I am trying to do. I am thinking of going through every line in the doInBackground(). If there is a better way to do it then let me know. But to begin with, why am I getting the EXCEPTION ?
Just to note.. I have simplified the code as much as I could so it would be easier to understand what I am trying to do.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyView myView = new
myView.play();
}
}
public class MyView{
public void play() {
new CustomAsync().execute();
}
}
class CustomAsync extends AsyncTask<Lyric, Void, Void> {
protected Void doInBackground(Lyric... param) {
startLyricCountDownTimer(param);
return null;
}
protected void onPostExecute(Void param) {
//Print Toast or open dialog
}
private void startLyricCountDownTimer(Lyric lyric){
new CountDownTimer(30000, 10) { //This is where it throws the error
public void onTick(long millisUntilFinished) {
//Do the thing
}
public void onFinish() {
}
}.start();
}
}
EDIT
Is it better to go with the AsyncTask and do like Son Truong suggested or to use the following code for each and every lrc line?
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
new CountDownTimer(millisInFuture,countDownInterval) {
#Override
public void onTick(
CountDownTimer uses a Handler to post messages to a Message Queue of a Thread which has a Looper. onTick and onFinish will be called on which thread based on where you create CountDownTimer instance.
In your case because you create CountDownTimer instance in doInBackground method of AsyncTask so these two methods will be call on AsyncTask thread.
In constructor of CountDownTimer, it will create Handler instance as well. The Handler will check whether or not current thread has a Looper, if not it will throw a RuntimeException with message.
Can't create handler inside thread that has not called
Looper.prepare()
Because AsyncTask uses a thread which has no Looper, that why your app crashes.
My suggestion is in doInBackground method you open a connection to .lrc file and read each line, for each line read, use runOnUIThread to send the line to UI thread (then you can process the line read there by display a Toast on screen, etc).
Update: I will demo how to read line by line from a file then display it on a text view each 3 seconds.
First write a class which read from an inputstream line by line
static class ReadLyricTask extends AsyncTask<InputStream, String, Void> {
WeakReference<MainActivity> mMainActivity;
ReadLyricTask(MainActivity activity) {
mMainActivity = new WeakReference<>(activity);
}
#Override
protected Void doInBackground(InputStream... inputStreams) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStreams[0]));
String line;
try {
while ((line = reader.readLine()) != null) {
publishProgress(line);
}
} catch (IOException e) {
// Do nothing.
} finally {
try {
inputStreams[0].close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void onProgressUpdate(String... values) {
MainActivity activity = mMainActivity.get();
if (activity != null) {
activity.displayLyricLineOnTextView(values[0]);
}
}
}
Then just use it in MainActivity
public class MainActivity extends AppCompatActivity {
private static final int UPDATE_LYRIC_TEXT_INTERVAL = 3000; // Change lyric text each 3 seconds.
private int mCurrentInterval = 0;
private TextView mLyricTextView;
private Handler mHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLyricTextView = findViewById(R.id.lyricText);
// I put a file named lyric.lrc in raw folder, for your case just open an input stream from a file.
InputStream inputStream = getResources().openRawResource(R.raw.lyric);
new ReadLyricTask(this).execute(inputStream);
}
private void displayLyricLineOnTextView(final String lyricLine) {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
mLyricTextView.setText(lyricLine);
}
}, mCurrentInterval);
mCurrentInterval += UPDATE_LYRIC_TEXT_INTERVAL;
}
}
CountDownTimer runs in separate thread and no need of asynctask to run a timer.Best solution would be create a service and make service to trigger a timer.
As timer run on non ui thread, while updating ui make sure you update from UI thread.You could use UI handler or runOnUithread method to update view.
I have a very simple test application I'm making, and to set the seekBar's position I'm using a runnable. Although I have very little experience with actually working with a runnable.
public class MySpotify extends Activity implements Runnable {
private SeekBar progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spotify_app);
myProgress = (SeekBar) findViewById(R.id.myBar);
}
#Override
public void run() {
myProgress.setProgress(25);
}
}
If I move myProgress.setProgress(25); into the onCreate then it works. But I want it to be set off in the runnable. Any ideas?
You need to post() a Runnable to a Thread for it to execute. Try calling post(this); inside onCreate().
Try
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spotify_app);
myProgress = (SeekBar) findViewById(R.id.myBar);
myProgress.post(new Runnable()
{
public void run()
{
myProgress.setProgress(25);
}
});
}
You need something to run the post() method on
You can start the run method by just calling run();
Be aware that it will execute on the main thread.
Also be aware that it will run only once since there is no loop.
if you want to update while doing something else you sjould create a new thread.
example:
public class MySpotify extends Activity{
private SeekBar myProgress; //I asume it is call "myProgress" instead of "progress"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spotify_app);
myProgress = (SeekBar) findViewById(R.id.myBar);
ThreadExample example = new ThreadExample();
example.start();
/* Start a new thread that executes the code in the thread by creating a new thread.
* If ou call example.run() it will execute on the mainthread so don't do that.
*/
}
private class ThreadExample extends Thread{
public void run() {
myProgress.setProgress(25);
}
}
}
All I am looking to do is run some function 5 seconds after the app starts. However i keep getting a "force close" after 5 sec. Is timerTask even the correct function to be using in a situation like this? How about if i want to press a button later in the program, and have an event occur 5 seconds after user presses the button?
package com.timertest;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class timerTest extends Activity {
Timer timer = new Timer();
TextView test;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
test = (TextView)findViewById(R.id.test);
timer.schedule(task, 5000);
}
TimerTask task = new TimerTask() {
#Override
public void run() {
test.setText("task function run");
}
};
}
A TimerTask will run on a background thread but you're trying to change UI. You want to run your operation on the UI thread instead. Android traditionally uses messages posted to a Handler to do this. A Handler will not create a new thread, when used as shown below it will simply attach to the same message queue that your app uses to process other incoming UI events.
public class Test extends Activity {
private final Handler mHandler = new Handler();
private TextView mTest;
private Runnable mTask = new Runnable() {
public void run() {
mTest.setText("task function run");
}
};
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTest = (TextView) findViewById(R.id.test);
mHandler.postDelayed(mTask, 5000);
}
#Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mTask);
}
}
If you want to execute some code after 5 secs try the following...
new CountDownTimer(5000,5000)
{
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
// DO YOUR OPERATION
}
}.start();