ScheduledThreadPoolExecutor does not work on Android. Why? - java

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.)

Related

Handler and thread/runnable

Can someone tell me why this doesn't work? I am trying to figure out how to use thread/runnable. Thread doesnt do much but just to loop and let the main thread know to update the text. I dont know what I missed, the centertext doesnt update. Thanks so much.
public class MainActivity extends AppCompatActivity {
TextView centerText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final SysTimeObj sysTimeObj = new SysTimeObj();
centerText = findViewById(R.id.centerText);
Handler stHandler = new Handler(getMainLooper()){
#Override
public void handleMessage(#NonNull Message msg) {
super.handleMessage(msg);
centerText.setText("thread updated");
}
};
startThread(sysTimeObj, stHandler);
}
public void startThread(SysTimeObj sysTimeObj, Handler handler){
clockThread rc = new clockThread(sysTimeObj, handler);
Thread t1 = new Thread(rc);
t1.start();
}
}
public class clockThread implements Runnable {
//private String sysTime;
private Handler handler;
SysTimeObj sysTimeObj;
public clockThread(SysTimeObj sysTimeObj, Handler mHandler){
//sysTime = GregorianCalendar.getInstance().getTime().toString();
this.sysTimeObj = sysTimeObj;
handler = mHandler;
}
#Override
public void run() {
sysTimeObj.setTime();
handler.postDelayed(this, 100);
}
}
You want to do something on the Main/UI Thread after a certain amount of time ? On Android, you don't need a new thread for that.
The Main Thread has a message queue that you can Post to. That message queue is emptied on a regular basis. Posted messages can be configured to be executed at a later time (which is what you seem to want).
To post messages, you need to create a Handler for the target thread. This Handler will let you send messages to that thread. Then, Post a Runnable to that thread using one of the posting methods availlable (here, postDelayed).
You'll end with something like this :
public class MainActivity extends AppCompatActivity {
private TextView yourTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yourTextView = findViewById(R.id.yourTextView);
Handler handler = new Handler(getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
yourTextView.setText("Updated after 100 ms");
}
}, 100);
}
}
If threads is really what you want, I suggest you look at AsyncTasks. You might also want to look at the official documentation about Process and Threads on Android Developpers.

Android Java Runnable Will Not Stop

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);
}

Run on UI thread from another class

I searched for a solution and couldn't find one so I'll ask here:
I'm trying to use setText command in the mainActivity, Until now I've used:
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
textViewPrograss.setText(finalI + "");
}
});
now I'm trying to do the same thing, but from another class so i cant use:MainActivity.this.
I was trying to use code i found on another question with no success,This is the code:
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
Log.d("UI thread", "I am the UI thread");
}});
Any suggestions?
This (the second code sample from your question) is the correct way to access UI Thread from random location, although you should always try to have a Context to do this :-)
new Handler(Looper.getMainLooper()).post(
new Runnable() {
#Override
public void run() {
Log.d("UI thread", "I am the UI thread");
}});
It does work, and if it does not, check if you have debug logs enabled in your debugger ^^
You can use this snippet
textView.post(new Runnable() {
#Override
public void run() {
textView.setText("Text");
}
});
Try this just pass the context to other class and then use it like this
((Activity)context).runOnUiThread(new Runnable() {
public void run() {
textViewPrograss.setText(finalI + "");
}
});
I suggest you tu use a BroadcastReceiver in the MainActivity. Register a new receiver with a specific action and send an Intent with that action from "another class". The MainActivity will receive the notification and can edit the TextView content in a clean way
MainActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// update your text view
String text = intent.getStringExtra("text");
}
};
registerReceiver(mReceiver, new IntentFilter(MY_ACTION));
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
Another class:
Intent intent = new Intent(MY_ACTION);
intent.putExtra("text", "Your wonderful text");
// take a context reference (e.g. mContext) if you don't have a getContext() method
getContext().sendBroadcast(intent);
This is my solution (using MVVM, so no business logic in activity), run this in your class (viewmodel in my case):
runOnUiThread(() -> methodToExecute());
here is the method implementation (I have this in my base viewmodel):
private Handler messageHandler;
protected void runOnUiThread(Runnable action) {
messageHandler.post(action);
}
Don't forget to init messageHandler:
messageHandler = new Handler(Looper.getMainLooper());

Java runnable doesn't seem to be triggered in Android

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);
}
}
}

Make this function repeat using runOnUiThread to update textview location

I trying to make my textView appear in different place of the screen every minute or two (delay is not important). I've seen people are suggesting I use runOnUiThread to make a timer repeat the random function and the update the UI.
I'm really struggling getting my head around these different threads, just wondering if anyone could give me an example? Or should I research using something different?
Public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.digitalClock1);
Random r = new Random();
int x = r.nextInt(350 - 100);
int y = r.nextInt(800 - 100);
textView.setX(x);
textView.setY(y);
}
Try this method
public void doInback()
{
handler.postDelayed(new Runnable() {
#Override
public void run()
{
// TODO Auto-generated method stub
// Try the code that you want to repeat
doInback();
}
}, 1000);
}
just call the method where you want to use.
Create the runnable and the handler below
private Runnable runnable = new Runnable(){
#Override
public void run() {
handler.sendEmptyMessage(0);
}
};
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
//change the text position here
this.postDelayed(runnable , TIME_OUT_MS);
}
};
The TIME_OUT_MS is the time out you want in milliseconds.
And put this on the OnCreate() method of the activity
Thread thread =new Thread(runnable );
thread.start();

Categories

Resources