In my case, I use AsyncTask.execute() method to connect to the Room database and change UI elements:
AsyncTask.execute(() -> {
Database db = Room.databaseBuilder(this.getApplicationContext(),
Database.class, "name-database").build();
Dao dao = db.getDao();
if (dao.findByNumber(1).isOpen) { // get data from the database
button.setBackgroundResource(R.drawable.active_button_shape) // change UI-element
}
});
Is this a thread-safe solution?, or need to create a separate class with an overridden onPostExecute() method for change UI? Thanks in advance!
SOLUTION
On the advice of Priyankagb I began to use runOnUiThread():
if (dao.findByNumber(1).isOpen) {
runOnUiThread(() -> button.setBackgroundResource(R.drawable.active_button_shape));
}
No this is not thread-safe. You have to use onPostExecute() or also you can use runOnUiThread() to change a button background into direct execute()
like...
runOnUiThread(new Runnable() {
#Override
public void run() {
button.setBackgroundResource(R.drawable.active_button_shape)
}
});
Related
My scenario is an onCreate() activity method which executes the following code (simplified):
dialog.show(); //loading wheel
try {
remote.sendRequest(myData, new MyHandler());
}
catch (Exception e) {
dialog.dismiss();
//log and react
}
class MyHandler extends SDKSpecificCompiledHandler {
#Override
public void failure() {
dialog.dismiss();
//do stuff
}
#override
public void success() {
dialog.dismiss();
//do stuff
}
}
//I have read-only access to this handler!
public abstract class SDKSpecificCompiledHandler {
public abstract void success(JSONObject successData);
public abstract void failure(JSONObject errorData);
}
Explanation: A remote service is called passing an handler that gets called when he's done. A loading wheel (dialog) is shown to the user until a success, failure or exception happens.
The problem is when the service gets successfully called but no response ever comes. In that case dialog.dismiss() doesn't get called and the loading wheel keeps spinning for ever.
What I need is a sort of timeout which dismisses the dialog (and possibly takes other actions) after some seconds if the server doesn't get back.
My first though would be to create a new thread for the service call, and right after the launch set a timer which dismisses the dialog.
Would it be a good idea?
Thank you,
EDIT:
The service is third-party/not editable. I'm using a pre-compiled artifact.
Still not really sure what you're trying to achieve but if you want to run some code after some time on main thread (i.e. your code will do stuff to the UI), you can use a android.os.Handler
mHandler = new Handler(getMainLooper());
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// do stuff on UI thread
}
},10000);
When your call returned from the server, simply cancel the messages on the queue:
mHandler.removeCallbacksAndMessages(null);
It is better to use time out in service call itself, You can set the time out with service , If you need know how to set the time out then I should know what kind of service you are using ?
One more thing is that if you are using a loader you should make that loader in such a way that it can be cancel by the client.
I am tried to search about AsyncTack. How to Update UI after asyntack execute.
Example : I have 2 java file.
file 1 : Myfragment.java, file 2 : MyAsyntack.java
in this case, i'm execute MyAsyntack on MyFragment. after Execute, I want to update UI.
On MyAsyntack, I'am updating database (doing in background). Any body can help?
You can set an interface in AsyncTask and call it in onPostExecute of it to update UI.
public inteface UpdateUI()
{
public void update();
}
then in your AsyncTask declare an updateUI like this:
UpdateUI updater;
public MyAsyntack (UpdateUI updater)
{
this.updater = updater;
}
#Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
updater.update();
}
and finally in your activity:
MyAsyntack myAsyncTask = new MyAsyntack(new UpdateUI(){
//how you want to update UI
}
);
myAsyncTask.execute();
doInBackground() is on worker thread and can be used to do heavy tasks like network calls to server.
When data is fetched, you can update the UI from onPostExecute(..) which is on the UI thread.
p.s. return of doInBackground will be argument to onPostExecute(..)
I want to delay my application for a while while a melody is playing, and when it's finished change the image on an imageview.
public void addListenerOnButtons() {
harmonicaTecknad= (ImageView)this.findViewById(R.id.harmonicatecknadspelautblas);
harmonicaTecknad.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
harmonicaTecknad.setImageResource(R.drawable.harmonicatecknadtryckrood);
RunAnimations();
utblas=MediaPlayer.create(SpelaTonerActivity.this, R.raw.utblas4);
utblas.start();
Thread timer = new Thread(){
public void run() {
try { // The delay should occur here
sleep(utblas.getDuration());
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
harmonicaTecknad.setImageResource(R.drawable.harmonicatecknad1);
}
}
};
timer.start();
}
}
I get an exception error, obviously I cannot set the image inside the thread, so where should I set it?
This is all explained in details, with examples, in the android documentation:
Android offers several ways to access the UI thread from other
threads. You may already be familiar with some of them but here is a
comprehensive list:
Activity.runOnUiThread(Runnable)
View.post(Runnable)
View.postDelayed(Runnable, long)
Handler
[...]
Unfortunately, these classes and methods could also tend to make your
code more complicated and more difficult to read. It becomes even
worse when your implement complex operations that require frequent UI
updates.
To remedy this problem, Android 1.5 and later platforms offer a
utility class called AsyncTask, that simplifies the creation of
long-running tasks that need to communicate with the user interface.
Write harmonicaTecknad.setImageResource(R.drawable.harmonicatecknad1); code on UI thread because you can not write UI code to non UI thread.
So simply replace above line of code with
runOnUiThread(new Runnable()
{
#Override
public void run()
{
harmonicaTecknad.setImageResource(R.drawable.harmonicatecknad1);
}
});
If you are still getting error then for testing purpose just change sleep(utblas.getDuration()); with sleep(1000);
As the other answers say, you need to set the image from the UI thread. However, while you can use Thread.sleep(), you should in general avoid using sleeps for logic. Most classes have some way to get callbacks when things happen - for example in your case you can use MediaPlayer.OnCompletionListener. You register for a callback from the media player, and that callback will always be on the UI thread. This is what it looks like:
public void addListenerOnButtons() {
harmonicaTecknad= (ImageView)this.findViewById(R.id.harmonicatecknadspelautblas);
harmonicaTecknad.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
harmonicaTecknad.setImageResource(R.drawable.harmonicatecknadtryckrood);
RunAnimations();
utblas=MediaPlayer.create(SpelaTonerActivity.this, R.raw.utblas4);
utblas.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
harmonicaTecknad.setImageResource(R.drawable.harmonicatecknad1);
}
};
utblas.start();
}
}
I have a WebView in my activity, which have a JSInterface :
mWebView.addJavascriptInterface(new JSInterface(mWebView, this), "interfaceWebsite");
When I call a function in my interface, I would like to see some Views (Textview/Button) modified.
public void changeStep(int newStep){
step = newStep;
tvStep.setText("Etape "+step);
step_info = getString(R.string.step3_info);
}
}
step_info works (my options menu change), it's just a string var, but not my TextView, it throw VM aborting.
I call the function this way (where addWebsiteActivity is "this" in the code above) :
addWebsiteActivity.changeStep(step);
Is this possible to do that in a proper way ?
Thanks
Only the original thread that created a view hierarchy can touch its views
Taht's why I can call changeStep from JSInterface but I can inside my activity class... How can I do that then ?
And the solution is...
runOnUiThread(new Runnable() {
public void run() {
step = newStep;
tvStep.setText("Etape "+step);
step_info = getString(R.string.step3_info);
}
});
Like this it knows it's a code for the main thread. But it's strange Eclipse throw me VM aborting exept that the explicit error above...
changeStep method must be called inside UI thread. You can achieved this with runOnUiThread:
addWebsiteActivity.runOnUiThread(new Runnable() {
public void run() {
changeStep(step);
}
})
change step to newStep in your code
i bascially have an activity
that calls an async task to set up the twitter classes, provided via twitter4j.
But i recieve an error regarding "cant create handler inside thread that has not called looper.prepare "
which is orginating from the TwitterApp class where there is a handler...
how can i get around this? successfully setting up the class no on the main UI thread as i used to have before (which worked perfectly but slowed down the app);
im basically doing:
new SetUpAsyncTaskt().execute();
within the asynctask all im doing is:
TwitterApp mTwitter;
postToTwitter = true;
String twitter_consumer_key="bllaalaa";
String twitter_secret_key="blaa"
private class SetUpAsyncTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
mTwitter = new TwitterApp(context, twitter_consumer_key,twitter_secret_key);
mTwitter.setListener(mTwLoginDialogListener);
return null;
}
#Override
protected void onPostExecute(Void result) {
if(!mTwitter.hasAccessToken()){
postToTwitter=false;
}
}
}
Thanks for any help!
UPDATE: After doing more testing, it seems the problem is due to the context, as if i remove all context based data within the class it works, but what i dont get is if i pass in the context from the UI thread, it still occurs ?? :S
UPDATE2: Found another way around it, thanks to all who replied.
Look here for the documentation: http://developer.android.com/reference/android/os/AsyncTask.html
some rules:
The task instance must be created on the UI thread.
execute(Params...) must be invoked on the UI thread.
My guess is you are executing the task on some different thread. To Execute it on UI thread create a Handler in onCreate and:
mHandler.post(new Runnable(){
//insert task creation & execution here
});
In this way the result that are in onPostExecute will be returned on the UI Thread too.
You can use runOnUiThread() to make the non-UI task run on the UI,
Try this,
#Override
protected Void doInBackground(Void... params) {
mTwitter = new TwitterApp(context, twitter_consumer_key,twitter_secret_key);
Activity_name.this.runOnUiThread(new Runnable() {
#Override
public void run() {
mTwitter.setListener(mTwLoginDialogListener);
}
});
return null;
}