I have tried to implement object in Android that would work in its own thread (I do not want to make handler public, I want to wrap sentMessage method with own public api). It has public methods to pass data to object. This object is associated with activity lifecycle (onResume, onPause). I would like to use looper and handler, not just pure Java thread with infinite loop. I want to start worker thread on resume and stop working on pause callback. Object has to wait for new message infinitely.
This is my code below:
public class ThreadingObject {
private MyThread thread;
public ThreadingObject() {}
public void onResume() {
thread = new MyThread();
thread.startWorking();
}
public void onPause() {
thread.stopWorking();
}
public void setMessage(Object object) {
Message msg = new Message();
msg.obj = object;
thread.handler.sendMessage(msg);
}
protected void work(Object object) {
//Do something with object in own thread
}
private class MyThread extends Thread {
public Handler handler;
#Override
public void run() {
Looper.prepare();
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
ThreadingObject.this.work((String[]) msg.obj);
}
};
Looper.loop();
}
public void startWorking() {
start();
}
public void stopWorking() {
handler.getLooper().quit();
}
}
}
Is it correct implementation? I receive warning: "sending message to a handler on a dead thread". Is there any issue that I do not see?
It is my implementation:
public class ThreadingObject {
private HandlerThread thread;
private Handler handler;
private Handler.Callback handlerCallback = new Handler.Callback() {
#Override
public boolean handleMessage(Message msg) {
work(msg.obj);
return true;
}
};
public ThreadingObject() {}
public void onResume() {
thread = new HandlerThread("SurfaceView HandlerThread");
thread.start();
handler = new Handler(thread.getLooper(), handlerCallback);
}
public void onPause() {
if(thread != null) {
thread.quit();
thread = null;
}
handler = null;
}
public void setMessage(Object object) {
Message msg = new Message();
msg.obj = object;
handler.sendMessage(msg);
}
protected void work(Object obj) {
//Working
}
}
In that run() method, if you need to re run or use loop, you need to add it by your self, ex.
and your error, is happen because you try to call the thread that already finish.
boolean aLoopLoopLoop = true;
Handler handler;
// this handler, you no need to declare it repeatedly, just declare it in onCreate() is enough
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
ThreadingObject.this.work((String[]) msg.obj);
}
};
// -----
#Override
public void run() {
Looper.prepare();
final Looper looper = Looper.myLooper();
while(aLoopLoopLoop) {
// write your code here
Looper.loop();
}
// after loop exit, quit loop
handler.postDelayed(new Runnable() {
#Override
public void run() {
looper.quit();
}
}, 3000);
}
// -----
public void stopWorking() {
aLoopLoopLoop = false;
}
Related
I am running a recursive handler which runs some code. I am posting the handler using a HandlerThread. I want to run the next recursive call only after the completion of the previous call.
Is it possible to do so? If not what are the alternatives.
HandlerThread ht = new HandlerThread();
ht.start();
Handler h = new Handler(ht.getLooper());
h.post(new Runnable() {
#override
public void run(){
//Some code
h.postDelay(this,1000);
}
});
Your code should work, but if you want a complete example how to run something recursively using HandlerThread, here it is:
public class Main2Activity extends AppCompatActivity {
private MyWorkerThread mWorkerThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mWorkerThread = new MyWorkerThread("myWorkerThread");
final Runnable task = new Runnable() {
#Override
public void run() {
Log.d("TAG", "Done.");
mWorkerThread.postTask(this);
}
};
mWorkerThread.start();
mWorkerThread.prepareHandler();
mWorkerThread.postTask(task);
}
#Override
protected void onDestroy() {
mWorkerThread.quit();
super.onDestroy();
}
}
class MyWorkerThread extends HandlerThread {
private Handler mWorkerHandler;
public MyWorkerThread(String name) {
super(name);
}
public void postTask(Runnable task){
mWorkerHandler.postDelayed(task, 1000); // set timeout which needed
}
public void prepareHandler(){
mWorkerHandler = new Handler(getLooper());
}
}
Don't forget to call handlerThread.quit() in onDestroy
When I had my logic inside a Runnable it worked fine except I could not interact with the UI Thread. So I am trying to put everything inside a class that extends Task and it works Except the task is only executed once. No errors and I get a succeeded message form the Task succeeded method.
I have also tried making the task return Boolean true in the call method but that did not help.
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
SyncTask syncTask = new SyncTask();
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(syncTask, 0, 10, TimeUnit.SECONDS);
Label syncEngineLabel = centralController.getScheduleTabMessageLabel();
syncEngineLabel.textProperty().bind(syncTask.messageProperty());
}
class SyncTask extends Task<Void>{
private Schedule schedule = null;
public SyncTask() {}
#Override
protected Void call() throws Exception {
System.out.println("we are in the task...");
if (getScheduleFromApi()){
updateMessage("New Schedule Retrieved...");
}
return null;
}
#Override protected void succeeded() {
super.succeeded();
System.out.println("succeeded");
}
#Override protected void cancelled() {
super.cancelled();
System.out.println("cancelled");
}
#Override protected void failed() {
super.failed();
System.out.println("failed");
}
private Boolean getScheduleFromApi(){
Gson gson = new GsonBuilder().serializeNulls().create();
ApiGet api = new ApiGet("schedule/get-schedule-by-room", parameters);
api.sendRequest();
if (api.isSuccess()){
schedule = gson.fromJson(api.response(), Schedule.class);
if (schedule.getStatus().equals("200")){
return true;
}
else{
updateMessage(schedule.getMsg());
return false;
}
}
else {
updateMessage("Failed to process API call to ASI Server.");
return false;
}
}
}
}
Please note that this code actually exists inside a controller but I put it in Main here to try and provide self contained code.
Thanks!
The ScheduledExecutorService will simply treat the task you provide as a Runnable, and try to reuse the same task instance every time it runs, which is explicitly forbidden in the documentation.
Use a ScheduledService instead:
#Override
public void start(Stage primaryStage) throws Exception{
ScheduledService<Void> scheduledService = new ScheduledService<Void>() {
#Override
protected Task<Void> createTask() {
return new SyncTask();
}
};
scheduledService.setPeriod(Duration.seconds(10));
scheduledService.start();
Label syncEngineLabel = centralController.getScheduleTabMessageLabel();
scheduledService.stateProperty().addListener((obs, oldState, newState) -> {
if (newState == Worker.State.RUNNING) {
syncEngineLabel.setText("Sync in progress");
} else if (newState == Worker.State.FAILED) {
syncEngineLabel.setText("Sync error");
} else {
syncEngineLabel.setText("Sync complete");
}
});
}
I want to define my runnable in my main thread and then pass it as an argument into another thread like below -
final Runnable runnable = new Runnable() {
#Override
public void run() {
Log.i("Running runnable","");
}
};
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
text.setText(msg.obj.toString());
}
};
ThreadTest handlerThreadTest = new ThreadTest(runnable, handler);
handlerThreadTest.start();
The ThreadTest class is defined as below -
public class ThreadTest extends Thread {
private Handler handler;
private Runnable runnable;
public ThreadTest(Runnable runnable, Handler handler) {
this.handler = handler;
this.runnable = runnable;
}
#Override
public void run() {
super.run();
runnable.run();
Message msg = handler.obtainMessage();
msg.obj = "YUSSSSSS!";
handler.sendMessage(msg);
} }
I'm able to send the "YUSSSSSS" message to my handler but runnable.run() does not seem to be working because I see nothing in logcat.
Where am I getting this wrong?
Logcat tends to erase empty messages, add some characters to your log and you may see something under the TAG of "Running runnable"
Log.i("RunnableImpl", "Running runnable");
I am updating progress bar with the help of handler of MainActivity
but it gives the error :Can't create handler inside thread that has not called Looper.prepare()
Error
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
Handler handler;
Thread thread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
thread = new Thread(new MyThread());
thread.start();
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
progressBar.setProgress(msg.arg1);
}
};
}
class MyThread implements Runnable {
#Override
public void run() {
Message message = Message.obtain();
for (int i = 0; i < 100; i++) {
message.arg1 = i;
handler.sendMessage(message);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
The error is because you are updating your UI from a different thread. So what you should be doing is like call the update method on main ui thread
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
runOnUiThread(new Runnable() {
#Override
public void run() {
// Do something on UiThread
// update progress
}
});
};
You should run progress bar only on Main Looper.
Add
handler = new Handler(context.getMainLooper()) {
.....
}
Should work.
I want to make a callback function in the ShipInfoManager to inform the MainActivity to do some action.
If I put onEvent() into Runnable, it runs.
However If I put it like this, it shows an error.
Is there any way to fire the callback after loading data?
Or, is there any way to do the callback like Android's API's LocatioManger's requestLocationUpdates, giving a callback when the data/variables is changed?
Thank you for any replies!
MyCallback Interface:
interface MyCallback {
void callbackCall();
}
ShipInfoManager class:
public class ShipInfoManager {
Context mContext;
public ShipInfoManager(Context _mContext) {
this.mContext = _mContext;
reloadData();
startTimer();
}
MyCallback callback;
void onEvent() {
callback.callbackCall();
}
private void startTimer() {
/* RUN EVERY MIN */
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#SuppressWarnings("unchecked")
public void run() {
try {
reloadData();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
// TEMP SUSPEND FROM PREVENTING RELOAD //
timer.schedule(doAsynchronousTask, 0, 5000);
}
/* JSON handling and extraction */
private void reloadData() {
//Do sth to reload the data
//After reload, I want to fire the callback
onEvent();
}
}
It looks like you haven't any listeners to your callback and you're not checking for this.
You should replace your ShipInfoManager with this:
public class ShipInfoManager {
public interface MyCallback {
void callbackCall();
}
public void setCustomEventListener(MyCallback eventListener) {
callback = eventListener;
}
Context mContext;
public ShipInfoManager(Context _mContext) {
this.mContext = _mContext;
reloadData();
startTimer();
}
MyCallback callback;
void onEvent() {
// Check if we have listeners
if (callback != null)
callback.callbackCall();
}
private void startTimer() {
/* RUN EVERY MIN */
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#SuppressWarnings("unchecked")
public void run() {
try {
reloadData();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
// TEMP SUSPEND FROM PREVENTING RELOAD //
timer.schedule(doAsynchronousTask, 0, 5000);
}
/* JSON handling and extraction */
private void reloadData() {
//Do sth to reload the data
//After reload, I want to fire the callback
onEvent();
}
}
Inside your Activity or Fragment you should have something like:
public class MainActivity extends ActionBarActivity implements ShipInfoManager.MyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShipInfoManager s = new ShipInfoManager(this);
s.setCustomEventListener(this);
}
#Override
public void callbackCall() {
}
}
I changed my MainActivity like this. It works now.
Thank you for your suggestion and reply!!!!!
public class MainActivity extends ActionBarActivity {
ShipInfoManager mShipInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShipInfo = new ShipInfoManager(this);
Log.i("Show Ship List Size", String.valueOf(mShipInfo.get_shipsList().size()));
Log.i("Show Ship - 6", String.valueOf(mShipInfo.getShip(6).getShip_name()));
mShipInfo.callback = new ShipInfoManager.MyCallback() {
#Override
public void callbackCall() {
Log.i("Call Back", "it is called");
}
};
}