Android: Pane tostato in un thread
-
01-10-2019 - |
Domanda
Come posso visualizzare Toast messaggi da un filo?
Soluzione
È possibile farlo chiamando il metodo Activity
di un runOnUiThread
dal tuo thread:
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
}
});
Altri suggerimenti
Mi piace avere un metodo nella mia attività chiamato showToast
che posso chiamare da qualsiasi ...
public void showToast(final String toast)
{
runOnUiThread(() -> Toast.makeText(MyActivity.this, toast, Toast.LENGTH_SHORT).show());
}
Ho poi più frequentemente chiamare dall'interno MyActivity
su qualsiasi thread come questo ...
showToast(getString(R.string.MyMessage));
Questo è simile ad altre risposte, ma aggiornato per nuove API disponibili e molto più pulito. Inoltre, non si assume che sei in un contesto di attività.
public class MyService extends AnyContextSubclass {
public void postToastMessage(final String message) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
}
});
}
}
Un approccio che funziona da praticamente ovunque, anche da luoghi in cui non si dispone di un Activity
o View
, è quello di afferrare un Handler
al thread principale e mostrare il brindisi:
public void toast(final Context context, final String text) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast.makeText(context, text, Toast.DURATION_LONG).show();
}
});
}
Il vantaggio di questo approccio è che funziona con qualsiasi Context
, tra cui Service
e Application
.
questo o questo , con un Runnable
che mostra il Toast
.
Vale a dire,
Activity activity = // reference to an Activity
// or
View view = // reference to a View
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
showToast(activity);
}
});
// or
view.post(new Runnable() {
@Override
public void run() {
showToast(view.getContext());
}
});
private void showToast(Context ctx) {
Toast.makeText(ctx, "Hi!", Toast.LENGTH_SHORT).show();
}
A volte, è necessario inviare un messaggio da un altro Thread
al thread UI. Questo tipo di scenario si verifica quando non è possibile eseguire operazioni di rete / IO sul thread dell'interfaccia utente.
Di seguito esempio maniglie quello scenario.
- Hai UI Discussione
- Devi iniziare il funzionamento IO e quindi non è possibile eseguire
Runnable
sul thread dell'interfaccia utente. Quindi pubblicare il tuoRunnable
al gestore suHandlerThread
- ottenere il risultato da
Runnable
e rinviarlo al thread UI e mostrare un messaggioToast
.
Soluzione:
- Crea un HandlerThread e avviarlo
- Crea una Handler con Looper da
HandlerThread
:requestHandler
- Creare un gestore con Looper da Discussione principale: metodo
responseHandler
e di overridehandleMessage
-
post
un compitoRunnable
surequestHandler
- All'interno compito
Runnable
, chiamatasendMessage
suresponseHandler
- Questo risultato
sendMessage
invocazionehandleMessage
inresponseHandler
. - Get attributi dalla
Message
e processi IT, l'aggiornamento dell'interfaccia utente
Codice di esempio:
/* Handler thread */
HandlerThread handlerThread = new HandlerThread("HandlerThread");
handlerThread.start();
Handler requestHandler = new Handler(handlerThread.getLooper());
final Handler responseHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
//txtView.setText((String) msg.obj);
Toast.makeText(MainActivity.this,
"Runnable on HandlerThread is completed and got result:"+(String)msg.obj,
Toast.LENGTH_LONG)
.show();
}
};
for ( int i=0; i<5; i++) {
Runnable myRunnable = new Runnable() {
@Override
public void run() {
try {
/* Add your business logic here and construct the
Messgae which should be handled in UI thread. For
example sake, just sending a simple Text here*/
String text = "" + (++rId);
Message msg = new Message();
msg.obj = text.toString();
responseHandler.sendMessage(msg);
System.out.println(text.toString());
} catch (Exception err) {
err.printStackTrace();
}
}
};
requestHandler.post(myRunnable);
}
Articoli utili:
handlerthreads-e-perché-si-deve-essere-utilizzando-li-a-la-android-app
- get UI filettatura esempio Handler e l'uso
handler.sendMessage();
- chiamare il metodo
post()
handler.post();
-
runOnUiThread()
-
view.post()
È possibile utilizzare Looper
per inviare un messaggio Toast
. Passare attraverso questo link per più dettagli.
public void showToastInThread(final Context context,final String str){
Looper.prepare();
MessageQueue queue = Looper.myQueue();
queue.addIdleHandler(new IdleHandler() {
int mReqCount = 0;
@Override
public boolean queueIdle() {
if (++mReqCount == 2) {
Looper.myLooper().quit();
return false;
} else
return true;
}
});
Toast.makeText(context, str,Toast.LENGTH_LONG).show();
Looper.loop();
}
e si chiama nel tuo thread. Contesto può essere Activity.getContext()
ottenere dal Activity
si deve mostrare il brindisi.
Ho fatto questo approccio basato sulla risposta mjaggard:
public static void toastAnywhere(final String text) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast.makeText(SuperApplication.getInstance().getApplicationContext(), text,
Toast.LENGTH_LONG).show();
}
});
}
funzionato bene per me.
ho incontrato lo stesso problema:
E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: com.example.languoguang.welcomeapp, PID: 4724
java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()
at android.widget.Toast$TN.<init>(Toast.java:393)
at android.widget.Toast.<init>(Toast.java:117)
at android.widget.Toast.makeText(Toast.java:280)
at android.widget.Toast.makeText(Toast.java:270)
at com.example.languoguang.welcomeapp.MainActivity$1.run(MainActivity.java:51)
at java.lang.Thread.run(Thread.java:764)
I/Process: Sending signal. PID: 4724 SIG: 9
Application terminated.
Prima: la funzione onCreate
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Toast.makeText(getBaseContext(), "Thread", Toast.LENGTH_LONG).show();
}
});
thread.start();
Dopo: la funzione onCreate
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getBaseContext(), "Thread", Toast.LENGTH_LONG).show();
}
});
ha funzionato.