public void onMessageReceived(RemoteMessage remoteMessage) {
try {
String type=remoteMessage.getData().get("type");
if(type.equals("1001")) {
CommonClass common = new CommonClass(getApplication());
CommonClass.MyTaskSendLog.execute(getApplicationContext(), DeviceDetails,lines);
}
} catch (Exception ex) {
}
}
this code gives error:
Method execute must be called from the main thread, currently inferred
thread is worker
I suppose your method is in a service.
To access UI thread (main thread) in a service you have to create an handler and call method inside as this:
if(type.equals("1001")) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
CommonClass common = new CommonClass(getApplication());
CommonClass.MyTaskSendLog.execute(getApplicationContext(), DeviceDetails,lines);
}
});
}
You could create Handler in onCreate of service.
Related
My Android app has a dedicated thread to make network api calls using retrofit.The thread makes a retrofit api call depending on the type of the message received. I would like to migrate this code to use Kotlin co-routines but I not sure how to implement message queues without using a Thread. Any help will be greatly appreciated.
Pseudo java code:
public class MyThread extends Thread {
private final Messenger mPushMessenger;
private Handler mHandler;
private Object mThreadLock;
private IMessage mMessage;
private MyThread(IMessage message) {
super(message.getThreadLink().name());
mMessage = message;
start();
mPushMessenger = new Messenger(getHandler());
mThreadLock = new Object();
}
public static MyThread getInstance(IMessage ISenThread)
{
return new MyThread(ISenThread);
}
public void NotifyThreadLock() {
synchronized (mThreadLock)
{
try {
mThreadLock.notify();
}
catch(Exception e)
{
}
}
}
private Handler getHandler() {
synchronized (this) {
while (mHandler == null) {
try {
wait();
} catch (InterruptedException ignored) {
}
}
}
return mHandler;
}
public void run() {
Looper.prepare();
synchronized (this) {
mHandler = new MyThread().MyHandler();
notifyAll();
}
Looper.loop();
}
public void pushMessage(Object message, int what) {
try {
android.os.Message msg = android.os.Message.obtain(null, what, message);
mPushMessenger.send(msg);
} catch (Exception ignored) {
}
}
public void EmptyQueue() {
}
/*
public void pushMessage(String message) {
pushMessage(message, SenFCMLink.SenEyeStatusSender.STATUS_GENERAL_MESSAGE);
}
*/
private class MyHandler extends Handler {
public void handleMessage(Message msg) {
switch(msg.Type)
{
case A:
// call retrofit API Method A
case B:
// call retrofit API Method B
case C:
// call retrofit API method C.
}
}
}
}
Retrofit methods can be declared for either synchronous or asynchronous execution
call.execute();
is a synchronous network call ,0
Synchronous requests are declared by defining a return type.Synchronous methods are executed on the main thread. That means the UI blocks during request execution and no interaction is possible for this period. Using the .execute() method on a call object will perform the synchronous request. The deserialized response body is available via the .body() method on the response object.
call.enqueue()
is a asynchronous network call, Asynchronous requests don’t have a return type. Instead, the defined method requires a typed callback as last method parameter.Using asynchronous requests forces you to implement a Callback with its two callback methods: success and failure. When calling the asynchronous getTasks() method from a service class, you have to implement a new Callback and define what should be done once the request finishes.
like below:
call.enqueue(new Callback<Object>() {
#Override
public void onResponse(Call<Object> call, Response<Object> response) {
response = response.body();
}
#Override
public void onFailure(Call<Object> call, Throwable t) {
}
I'm beginner in android and trying to show toast in the async task, for that purpose I wrote this code:
public class GetReading {
public GetReading() {
}
public List<ReadingModel> Get(String TokenKey, Context adapter) throws ExecutionException, InterruptedException {
GetReadingTask params = new GetReadingTask(TokenKey, adapter);
List List_result = (List)(new GetReading.AsyncRead()).execute(new GetReadingTask[]{params}).get();
return List_result;
}
private class AsyncRead extends AsyncTask<GetReadingTask, Void, List<ReadingModel>> {
ir.behineh.wepapiinterface.GETREADINGINTERFACE.ReadingModel.List x;
private AsyncRead() {
}
protected List<ReadingModel> doInBackground(GetReadingTask... getReadingTasks) {
final Context pos = getReadingTasks[0].adapter;
Handler handler = new Handler(pos.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast.makeText(pos, "Created a server socket", 1).show();
}
});
ir.behineh.wepapiinterface.GETREADINGINTERFACE.GetReading taskService = (ir.behineh.wepapiinterface.GETREADINGINTERFACE.GetReading)ServiceGenerator.createService(ir.behineh.wepapiinterface.GETREADINGINTERFACE.GetReading.class);
Call tasks = taskService.getReadings("application/x-www-form-urlencoded", "application/json", "bearer " + getReadingTasks[0].TokenKey);
try {
this.x = (ir.behineh.wepapiinterface.GETREADINGINTERFACE.ReadingModel.List)tasks.execute().body();
} catch (IOException var7) {
var7.printStackTrace();
}
return this.x;
}
}
}
and when i try to call that async task with this code:
GetReading reading=new GetReading();
List<ReadingModel> result= reading.Get("VQ",LineActivity.this);
after finishing doinbackground get the toast, but i want to show toast first to user, what happen? how can i solve that problem? thanks all.
Toasts as well as anything that has to do with the UI cannot be fired from any thread that runs in the background.
Move your code for displaying the toast to either onProgressUpdate or on onPostExecute.
All your troubles arise because you use the .get() in
execute(new GetReadingTask[]{params}).get();
Never use .get() as it kills the asynchonity of your task.
Instead: do the things you want to do with the result in onPostExecute().
I keep getting:
java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
However the thread has been interrupted by the time it even touches a realm object, any way of me fixing this?
RealmConfiguration config = new RealmConfiguration.Builder()
.name("CustomSchedule.realm")
.schemaVersion(42)
.build();
mRealm = Realm.getInstance(config);
mDays = app.getmDays();
final Handler handler = new Handler();
t = new Thread(new Runnable() {
public void run() {
while(!Thread.currentThread().isInterrupted()) {
while (mDays.isEmpty()) {
}
Thread.currentThread().interrupt();
startActivity();
handler.post(this);
}
}
});
t.start();
return rootView;
}
Within the startActivity section I reference realm objects as normal.
I changed the Thread to:
final Handler handler = new Handler();
final Thread t = new Thread(new Runnable() {
public void run() {
while(!Thread.currentThread().isInterrupted()) {
while (mDays.isEmpty()) {
}
handler.post(new Runnable() {
#Override
public void run() {
startActivity();
}
});
Thread.currentThread().interrupt();
}
}
});
t.start();
And now it works.
I load my articles from my wordpress webpage. And want to display them in my app.
In order to be allowed to wait for a internet response I had to create a Thread.
Using this method:
private void loadArticles(final String url) {
Thread thread = new Thread(new Runnable(){
#Override
public void run() {
try {
//Get my data and run createArticles();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
Then in the createArticles function I try to use my function addViewToParent(...) and I get an error that says:
Only the original thread that created a view hierarchy can touch its
views.
How can i work around this problem?
EDIT: I use these networking functions if it matters...
//Connect to the url
URL pageURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection) pageURL.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream())
);
If you want to modify your Views, e.g. show your article, you have to run the code on the UI/main thread. To achieve this, you could call run runOnUiThread() in your Activity:
private void loadArticles(final String url) {
Thread thread = new Thread(new Runnable(){
#Override
public void run() {
try {
//Get my data ...
runOnUiThread(new Runnable() {
public void run() {
// modify Views here
createArticles();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
You need to use function runOnUiThread. This is function for user interface thread.
private void loadArticles(final String url) {
//Get my data ...
runOnUiThread(new Runnable() {
public void run() {
// modify Views here
createArticles();
}
});
});
Just use a Handler instead, so you'll have access to the UI Thread:
new Handler().post(new Runnable(){
#Override
public void run() {
try {
//Get my data and run createArticles();
} catch (Exception e) {
e.printStackTrace();
}
}
});
In Handler, we can pass some data from a background thread to the UI thread like this:
private void someBackgroundThreadOperation() {
final String data = "hello";
handler.post(new Runnable() {
public void run() {
Log.d(TAG, "Message from bg thread: " + data);
}
}
}
If we use the above, we cannot then use Handler.removeCallbacks(Runnable r), because we won't have references to any of the anonymous runnables we created above.
We could create a single Runnable instance, and post that to the handler, but it won't allow us to pass any data through:
private void someBackgroundThreadOperation() {
String data = "hello";
handler.post(mRunnable); // can't pass 'data' through.
}
private Runnable mRunnable = new Runnable() {
public void run() {
Log.d(TAG, "What data?");
}
}
We can however use the Handler.removeCallbacks(mRunnable) method then, since we have a reference to it.
I know I can setup some synchronization myself to get the second method working, but I'm wondering if Handler offers any utility for passing data through to a single referenced Runnable, almost like in the example above?
The closest I can think of from browsing the docs is to use something like:
private void someUiThreadSetup() {
mHandler = new Handler(new Callback() {
#Override
public boolean handleMessage(Message msg) {
String data = (String)msg.obj;
return false;
}
});
}
private void someBackgroundThreadOperation() {
String data = "please work";
Message msg = mHandler.obtain(0, data);
mHandler.sendMessage(msg);
}
private void cleanup() {
mHandler.removeMessages(0);
}
Is this the proper way of doing it?
Thanks
IMHO, the pattern you seek is packaged as the AsyncTask class.