React-Native NativeModules - Is there an Android/Java callback pattern - java

In my react-native JS code I'm calling a Native Module and it was blocking the UI thread for about 1.5sec. Running it on a different thread with a Runnable works but I can't capture the returned value that happens inside the Runnable?
#ReactMethod
public void print(final String printerAddress, final String price, final String description, final String upc, Promise promise) {
try {
boolean success = false;
new Thread(new Runnable() {
public void run() {
success = mEpsonPrinter.printLabel(printerAddress, price, description, upc);
}
}).start();
promise.resolve(success);
} catch (IllegalViewOperationException e) {
promise.reject(e);
}
}

To resolve the immediate problem I placed the promise.resolve(success) call inside the Runnable.run()
try {
new Thread(new Runnable() {
public void run() {
boolean success = mEpsonPrinter.printLabel(printerAddress, price, description, upc);
promise.resolve(success);
}
}).start();
} catch (IllegalViewOperationException e) {
promise.reject(e);
}
Although I'm still left with questioning a solution for callback pattern with Java.

Related

Null textview when it's call on a handler

I'm trying to do a setText() on a Textview (already instantiate in the onCreate()) called by a Handler and using the ruiOnUiTread() but I have a nullPointerException on the Textview.
Where can the problem come from?
I saw in the debug that the instance of the activity was not the same between the instantiation and the setText() while I do not change activity but impossible to instantiate it in the same place as the setText().
private TextView ambianceTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ambianceTextView = findViewById(R.id.valeur_ambiance);
StarterKillerPidroid.getInstance().startApp();
}
private final Runnable retrieveData = new Runnable() {
public void run() {
try {
setText();
} catch (Exception e) {
e.printStackTrace();
}
handlerRecup.postDelayed(retrieveData, 1000);
}
};
public void setText(){
runOnUiThread(new Runnable() {
#Override
public void run() {
ambianceTextView.setText("test");
}
});
}
public void doAfterLogin() {
handlerRecup.postDelayed(retrieveData, 10000);
}
the runnable is started by a function called by a callback of an http request with Volley
public class StarterKillerPidroid {
void startApp() {
//Sending a request
PostmanPidroid.getInstance().login();
}
public void ackLogin(Boolean isValid) {
if (isValid) {
ActivityMain.getInstance().doAfterLogin();
} else {
PostmanPidroid.getInstance().login();
}
}
}
The class Postman :
public class Postman {
public void login(){
// Parameters
String email = "test#tes";
String password = "test";
// Encoding the request with parameters
JsonObjectRequest request = EncoderDecoderPidroid.getInstance()
.encodeRequestLogin(email, password);
// Sending the request
sendRequest(request);
}
void sendRequest(StringRequest message){
// Creating the queu if it's not create
if (queue == null) {
queue = Volley.newRequestQueue(context);
}
// Adding the request to the queue
queue.add(message);
}
}
When a success response is received, this callback is called :
private Response.Listener<JSONObject> callbackLogin =
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
...
StarterKillerPidroid.getInstance().ackLogin(true);
}
};
Basically, this kind of problem is due to the instance. It may be possible that your textview instance is not initialized. One more thing using handler directly for updating UI thread is not a good idea. Instead of directly updating Ui with handler you should use FunctionalInterface for doing this.
FunctionalInterface is a good approach for such cases.
A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. ... Runnable, ActionListener, Comparable are some of the examples of functional interfaces.
Java has a predefined FunctionalInterface Callable. It goes something like this
public static void doDid(final Callable<Void> callable) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
try {
callable.call();
handler.postDelayed(this, every * repeattime);
} catch (Exception e) {
e.printStackTrace();
}
}
}, every * tempvalue);
}
and use this for updating UI in this way
doDid(new Callable<Void>() {
#Override
public Void call() {
textView.setText("Your text");
return null;
}
});
There is one open-source library available for Android which works like a charm is such cases called Predictor. You can download it from here and import in your project. You can also contribute in this project for saving many developers life.
Do you wanna see how predictor can do this?
Predictor.every(3).second().doDid(new Job<Void>() {
#Override
public Void run() {
textView.setText("Your text");
return null;
}
});
What can you do with predictor?
Predictor gives you several ways of handling multithreading some of them are as follows:
Predictor.every(3).second().doDid(something());
Predictor.every(5).minutes().doDid(something());
Predictor.every().hour().doDid(something());
Predictor.every().week().doDid(something());
Predictor.every().month().doDid(something());
and many more...
Try this way:
private final Runnable retrieveData = new Runnable() {
public void run() {
try {
ambianceTextView = (TextView) findViewById(R.id.valeur_ambiance);
setText();
} catch (Exception e) {
e.printStackTrace();
}
handlerRecup.postDelayed(retrieveData, 1000);
}
};

Looper.prepare() error inside a thread

I am using handler to get GCM value
I want to update this value in my database
so I call AsyncTask from the handler
but I get this Error
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
I checked other solutions they said I have to put the code in the run() section which I already do..
This is the code,
private void GetGCM(final String UserID) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
GCMHelper gcmRegistrationHelper = new GCMHelper(getApplicationContext());
String gcmRegID = "";
gcmRegID = gcmRegistrationHelper.GCMRegister("123456");
// Update using Web Service
try {
UpdateGCMWSTask updateGCMWSTask = new UpdateGCMWSTask();
updateGCMWSTask.execute(UserID, gcmRegID);
// ************ HERE IS THE ERROR ***********************
}catch (Exception e)
{
e.printStackTrace();
}
} catch (Exception bug) {
bug.printStackTrace();
}
}
});
thread.start();
}
Add Looper.prepare() and Looper.loop() in you code, like this:
private void GetGCM(final String UserID) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
Looper.prepare();
GCMHelper gcmRegistrationHelper = new GCMHelper(getApplicationContext());
String gcmRegID = "";
gcmRegID = gcmRegistrationHelper.GCMRegister("123456");
// Update using Web Service
try {
UpdateGCMWSTask updateGCMWSTask = new UpdateGCMWSTask();
updateGCMWSTask.execute(UserID, gcmRegID);
// ************ HERE IS THE ERROR ***********************
}catch (Exception e)
{
e.printStackTrace();
}
Looper.loop();
} catch (Exception bug) {
bug.printStackTrace();
}
}
});
thread.start();
}
You can't create asynctask inside a thread. There are few ways to handle it:
Create a new handler.
Call function runOnUIThread of activity.
Using broadcast.

Android : Network Main exception in thread

I did this code to change the tabs name from HTML but i got this error :
android.os.NetworkOnMainThreadException
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork
I know you can't do networks operations on the main thread but here i use runOnUiThread so it should be this error.
Thread thread = new Thread()
{
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run()
{
try
{
Document doc = Jsoup.connect("http://terry.gonguet.com/cal/?g=tp11").get();
Elements days = doc.select("div.day");
for (Element day : days)
{
String jour = day.getElementsByClass("dayDate").first().html();
mSectionsPagerAdapter.AddFragment(new MainFragment().newInstance(1), jour);
}
}catch (IOException ex){}
}
});
}
};
thread.start();
I know you can't do networks operations on the main thread
You clearly are doing network operation in the main thread since runOnUiThread will run on the main thread and you are calling Jsoup.connect which is a network connection.
You need to either create an AsyncTask which is preferable than a Thread; unless you are doing a long running task.
You can not run network operations on the UI thread because your interface will freeze until the finish of network operatin, you can use AsyncTask for this, it is the perfect solution for this kind of operations. With AsyncTask you can run a network operation and update the UI after (and even during) the network operation. For example:
new AsyncTask<String, String, Elements>(){
#Override
protected Elements doInBackground(String... params) {
Document doc = Jsoup.connect("http://terry.gonguet.com/cal/?g=tp11").get();
Elements days = doc.select("div.day");
return days;
}
#Override
protected void onPostExecute(Elements days) {
for (Element day : days) {
String jour = day.getElementsByClass("dayDate").first().html();
mSectionsPagerAdapter.AddFragment(new MainFragment().newInstance(1), jour);
}
}
}.execute();
You should only add the fragments inside the UI thread but not the request so this should work:
Thread thread = new Thread() {
#Override
public void run() {
try
{
Document doc = Jsoup.connect("http://terry.gonguet.com/cal/?g=tp11").get();
Elements days = doc.select("div.day");
runOnUiThread(new Runnable() {
#Override
public void run()
{
for (Element day : days)
{
String jour = day.getElementsByClass("dayDate").first().html();
mSectionsPagerAdapter.AddFragment(new MainFragment().newInstance(1), jour);
}
}
});
} catch (IOException ex){}
}
};
thread.start();

How to make an Android class wait until another class completes its task?

I'm writing an Android messaging application, and 1 class is calling another class, and I wish for the calling class to wait for the callee class to complete before carrying on.
Caller Class (MessageManagement) code snippet is as follows:
private static Messenger myMessenger;
try {
Message msg = Message.obtain();
msg.arg1 = constructedMessage.length();
msg.arg2 = -1;
msg.obj = constructedMessage;
Log.d(TAG, "Calling myMessenger.send()");
myMessenger.send(msg);
Log.d(TAG, "Sent");
} catch (Exception e) {
e.printStackTrace();
}
// Wait here until myMessenger completes its task
doOtherStuff();
Right now, doOtherStuff() starts and finishes before myMessenger starts. I need myMessenger to complete before doOtherStuff() starts.
I've read about wait() and notify() but I'm not sure how to implement it here, or whether it's the right choice.
Some background about the flow of the program. It's basically a messaging app that I inherited, so I'm not exactly sure of its framework. From what I can tell tracing the flow of the code:
When an SMS message is received, the SMS receiver BroadcastReceiver(SmsReceiver) handles it, getting the sender address and message body, then calling a SMS handler service(HandleSmsService), which then calls the caller class in a runnable with the following code:
HandleSmsService
public class HandleSmsService extends Service {
private String message;
private MessageManagement messageManager;
private Handler timeoutHandler = new Handler();
#Override
public void onStart(Intent intent, intent startid) {
message = intent.getExtras().getString("message");
messageManager = new MessageManagement(this);
timeoutHandler.postDelayed(runnable, 10);
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
try {
messageManager.handleMessage(message);
stopSelf();
} catch (Exception e) {
e.printStackTrace();
}
}
};
MessageManagement is my caller class, and MessageManagement.handleMessage() is the top most code snippet presented earlier.
The MessageManagement.handleMessage() apparently calls another Handler in the callee class when it calls myMessenger.send(msg). This Handler code is as follows:
private Handler smsHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
// do some stuff
}
};
I'm assuming the posted code is running on the MainThread and the reason why you are using a handler is that something asynchronous is done on another thread when receiving that message.
In that case, you can't use wait on the thread, as it will lock up the UI and probably cause an application not responding error.
Without changing too much of your code, one way to do it is to nest a listener in your constructedMessage for e.g.
public class DoStuffRequest {
private OnFinishListener mOnFinishListener;
private boolean isCanceled;
private String mMessage;
public interface OnFinishListener {
public void onFinish();
}
public DoStuffRequest(String message) {
mMessage = message;
}
public OnFinishListener getOnFinishListener() {
return mOnFinishListener;
}
public void setOnFinishListener(OnFinishListener onFinishListener) {
mOnFinishListener = onFinishListener;
}
public void cancel() {
isCanceled = true;
}
public void notifyFinish() {
if (!isCanceled && mOnFinishListener != null) {
mOnFinishListener.onFinish();
}
}
public String getMessage() {
return mMessage;
}
}
then use some along the line of this to get the ball rolling:
private static Messenger myMessenger;
private DoStuffRequest mRequest;
...
private void send(String message) {
mRequest = new DoStuffRequest(message);
mRequest.setOnFinishListener(new ConstructedMessage.OnFinishListener() {
#Override
public void onFinish() {
doOtherStuff();
}
});
try {
Message msg = Message.obtain();
msg.arg1 = constructedMessage.length();
msg.arg2 = -1;
msg.obj = constructedMessage;
Log.d(TAG, "Calling myMessenger.send()");
myMessenger.send(msg);
Log.d(TAG, "Sent");
} catch (Exception e) {
e.printStackTrace();
}
}
private void doThisIfYouWantToCancel() {
if (mRequest != null) {
mRequest.cancel();
}
}
your Handler/Service code can now call constructedMessage.finish() when the async stuff is done. Depending on what doOtherStuff() does (e.g. when manipulating the UI), you might want to do this on the MainThread (the code i've written above is NOT thread safe and i assume you are calling the listener on the MainThread).
Also remember to call constructedMessage.cancel() in case you do not want to get notified any more (e.g. you are leaving the activity/fragment).
this is just one way to do it, depending on your needs, some other methods might be a better choice.
I guess it should look something like this:
try {
Message msg = Message.obtain(handler, new Runnable() {
#Override
public void run() {
doOtherStuff();
}
});
msg.arg1 = constructedMessage.length();
msg.arg2 = -1;
msg.obj = constructedMessage;
Log.d(TAG, "Calling myMessenger.send()");
msg.sendToTarget();
Log.d(TAG, "Sent");
} catch (Exception e) {
e.printStackTrace();
}
The other way to do this using native means:
private static Messenger myMessenger = new Messenger(new Handler(new Handler.Callback() {
#Override
public boolean handleMessage(Message msg) {
// do something what you need
if (msg.getTarget() != null) {
msg.sendToTarget();
}
return false;
}
}));
try {
final Message msg = Message.obtain();
msg.setTarget(new Handler(new Handler.Callback() {
#Override
public boolean handleMessage(Message msg) {
doOtherStuff();
return false;
}
}));
msg.arg1 = constructedMessage.length();
msg.arg2 = -1;
msg.obj = constructedMessage;
Log.d(TAG, "Calling myMessenger.send()");
myMessenger.send(msg);
Log.d(TAG, "Sent");
} catch (final Exception e) {
e.printStackTrace();
}

Why is my GUI unresponsive while a SwingWorker thread runs?

I have a SwingWorker thread with an IOBound task which is totally locking up the interface while it runs. Swapping out the normal workload for a counter loop has the same result. The SwingWorker looks basically like this:
public class BackupWorker extends SwingWorker<String, String> {
private static String uname = null;
private static String pass = null;
private static String filename = null;
static String status = null;
BackupWorker (String uname, String pass, String filename) {
this.uname = uname;
this.pass = pass;
this.filename = filename;
}
#Override
protected String doInBackground() throws Exception {
BackupObject bak = newBackupObject(uname,pass,filename);
return "Done!";
}
}
The code that kicks it off lives in a class that extends JFrame:
public void actionPerformed(ActionEvent event) {
String cmd = event.getActionCommand();
if (BACKUP.equals(cmd)) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final StatusFrame statusFrame = new StatusFrame();
statusFrame.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
public void run () {
statusFrame.beginBackup(uname,pass,filename);
}
});
}
});
}
}
Here's the interesting part of StatusFrame:
public void beginBackup(final String uname, final String pass, final String filename) {
worker = new BackupWorker(uname, pass, filename);
worker.execute();
try {
System.out.println(worker.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
So far as I can see, everything "long-running" is handled by the worker, and everything that touches the GUI on the EDT. Have I tangled things up somewhere, or am I expecting too much of SwingWorker?
I think the problem is due to the call to SwingWorker.get() in your beginBackup method. Take a look at the docs for this method:
Waits if necessary for the computation
to complete, and then retrieves its
result.
This is a blocking call, hence your GUI becomes unresponsive.
(Also, is there any particular reason why you're doing an invokeLater from within an invokeLater call? You're already running on the EDT.)
Read the section from the Swing tutorial on Tasks That Have Interim Results for a working example. You will see that the get(...) method is invoked from within the process(...) method that is overridden in the SwingWorker class.

Categories

Resources