How to display Toast from a Service after main Activity finishes? - android

UPDATE: I don't agree that this is a duplicate - because I am seeking for a way to exit the main app and still show a Toast from the service.
In a very simple test app I have 2 buttons:
Clicking any of the buttons will run a service with a corresponding action string ("open" or "flash") -
OpenActivity.java:
public class OpenActivity extends Activity {
private Intent mServiceIntent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
mServiceIntent = new Intent(this, RegionService.class);
}
public void openCar(View v) {
mServiceIntent.setAction("open");
startService(mServiceIntent);
}
RegionService.java:
public class RegionService extends IntentService {
private static final String TAG = "RegionService";
#Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "Received an intent: " + intent);
String action = intent.getAction();
Log.d(TAG, "Received an action: " + action);
if(action.equals("open")) {
Toast.makeText(this,
getString(R.string.car_opened),
Toast.LENGTH_SHORT).show();
}
Unfortunately my app crashes with:
D/RegionService(24506): Received an intent: Intent { act=open cmp=de.afarber.mynotification/.RegionService }
D/RegionService(24506): Received an action: open
W/MessageQueue(24506): Handler (android.os.Handler) {422768a8} sending message to a Handler on a dead thread
W/MessageQueue(24506): java.lang.RuntimeException: Handler (android.os.Handler) {422768a8} sending message to a Handler on a dead thread
W/MessageQueue(24506): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:320)
W/MessageQueue(24506): at android.os.Handler.enqueueMessage(Handler.java:626)
W/MessageQueue(24506): at android.os.Handler.sendMessageAtTime(Handler.java:595)
W/MessageQueue(24506): at android.os.Handler.sendMessageDelayed(Handler.java:566)
W/MessageQueue(24506): at android.os.Handler.post(Handler.java:326)
W/MessageQueue(24506): at android.widget.Toast$TN.hide(Toast.java:370)
W/MessageQueue(24506): at android.app.ITransientNotification$Stub.onTransact(ITransientNotification.java:54)
W/MessageQueue(24506): at android.os.Binder.execTransact(Binder.java:412)
W/MessageQueue(24506): at dalvik.system.NativeStart.run(Native Method)
Being an Android programming newbie I wonder How to display a Toast from Service in a correct way?
I think I've already seen Toasts at Android Home (i.e. there was no Activity on the device screen and still there were Toasts).
My background: I would like to monitor a beacon device from my service and show some text Toasts - even when my app has been closed.

OnHandleIntent will run in a differant Thread
so you are showing Toast in a thread which is not allowed in android
so change your code like this
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
getString(R.string.car_opened),
Toast.LENGTH_SHORT).show();
}
});
From this dead thread in service
IntentService will create a thread to handle the new intent, and terminated it immediately once the task has done. So, the Toast will be out of controlled by a dead thread.
You should see some exceptions in the console when the toast showing on the screen.

An IntentService has a few limitations:
It can't interact directly with your user interface. To put its
results in the UI, you have to send them to an Activity.
Everything is happening in the background thread and not on the UI thread, so you need a different way as shown below:
#Override
public void onCreate() {
super.onCreate();
mHandler = new Handler();
}
#Override
protected void onHandleIntent(Intent intent) {
mHandler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(MyIntentService.this, "Hello Toast!", Toast.LENGTH_LONG).show();
}
});
}
Source: Toast created in an IntentService never goes away

OnHandleIntent is called on a background thread, and any attempt to touch the UI will result in a crash. Use an Handler to post a Runnable on the UI Thread to show your toast.
private class MyRunnable implements Runnable {
final int mTextId = -1;
final Context mContext;
public MyRunnable(Context c, int textId) {
mTextId = textId;
mContext = c;
}
#Override
public void run() {
Toast.makeText(mContext,
getString(mTextId),
Toast.LENGTH_SHORT).show();
}
}
Handler handler = new Handler();
handler.post(new MyRunnable(this, R.string.car_opened));

use the following code:
runOnUiThread(new Runnable(){
public void run() {
// UI code goes here
}
});

This problem because of not running the Toast from the main_thread, and to overcome that,
when creating the Activity save it's context from onCreate() method:
public static Context ctx;
// the method responsible for running the MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
}
then, in the service add a handler and run a thread from it(as the Handler is executed through the main Thread):
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(OpenActivity.ctx, getString(R.string.car_opened),
Toast.LENGTH_SHORT).show();
}
});

Related

why my Handler doesn't work with UI thread

I'm trying to understand how Handler works in a pair with Looper, but i have some problem. I need to do some long operation in a back thread and then to send some result in a textView.
I get the following error after pressing a button:
Only the original thread that created a view hierarchy can touch its views.
public class MainActivity extends AppCompatActivity {
Button mButton;
TextView mTextView;
ConsumeThread mConsumeThread;
class ConsumeThread extends Thread{
public Handler mHandler;
#Override
public void run() {
Looper.prepare();
mHandler = new Handler(){
#Override
public void handleMessage(Message msg){
int arg = msg.what;
someLongOperation(arg);
}
};
Looper.loop();
}
private void someLongOperation(int arg){
// do some long operation
arg += 1000;
mTextView.setText("Operation's code is " +arg); // fatal exception
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.txt_view);
mButton = (Button) findViewById(R.id.button);
mConsumeThread = new ConsumeThread();
mConsumeThread.start();
mButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
if(mConsumeThread.mHandler != null){
Message msg = mConsumeThread.mHandler.obtainMessage(10);
mConsumeThread.mHandler.sendMessage(msg);
}
}
});
}
To get Main Thread Handler You have get Handler as follows .
Because:-
Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it
So you need to get Handler which is associated with MainThread. For that you can use one of the following:-
With Context
Handler mainHandler = new Handler(getMainLooper()){
#Override
public void handleMessage(Message msg) {
}
};
Or Directly with Looper even when do not have Context
Handler mainHandler = new Handler(Looper.getMainLooper()){
#Override
public void handleMessage(Message msg) {
}
};
I assume that you are doing some long running task . So its better if you go with AsyncTask.
private void someLongOperation(int arg){
// do some long operation
arg =+ 1000;
mTextView.setText("Operation's code is " +arg); // fatal exception
}
//see here, you are in worker thread, so you can't excess UI toolkit or else exception, so if you want to do something ui related task in worker thread, use runOnUi, see here
runOnUiThread(new Runnable() {
#Override
public void run() {
textview.setText("");
}
})
You can't update the UI from another thread. You have to move the code that updates the UI to the UIThread.
Try Using:
runOnUiThread(new Runnable() {
#Override
public void run() {
//TextView Update Code
}
});
Tip: Try to reduce the number of lines of code you put inside this, as then there would be no purpose of using another thread.
Only the original thread that created a view hierarchy can touch its views
You have to do ui related work on the main thread...
So you can do it like this...
private void someLongOperation(int arg){
// do some long operation
arg =+ 1000;
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
mTextView.setText("Operation's code is " +arg); // fatal exception
}
});
}

Android UI thread update from web service

If I want to update my UI from a background thread when a message is received from a web service call, would the below be considered a safe option? I'm worried about potential memory leaks having the handler in my Application class
public class MyApplication extends Application {
private static long uiThreadId;
private static Handler uiHandler;
#Override
public void onCreate() {
super.onCreate();
uiThreadId = Thread.currentThread().getId();
uiHandler = new Handler();
}
public static void customRunOnUiThread(Runnable action) {
if (Thread.currentThread().getId() != uiThreadId) {
uiHandler.post(action);
} else {
action.run();
}
} }
And then in the class that deals with the messages received in the separate threads:
new Thread(
new Runnable() {
#Override
public void run() {
// make web service call
MyApplication.customRunOnUiThread(new Runnable() {
#Override
public void run() {
httpResponseHandler.onSuccess();
}
});
}
}
}).start();
My alternative is to use parallel AsyncTasks doing the work in the doInBackground method and updating the UI in the onPostExecute method. Both of these options work, but I'm not sure which one is 'most' correct so to speak.
Better way is to register BroadcastReceiver and using LocalBroadcastManager send the broadcast on receiving any message

Passing Message to background thread causes corruption

I am trying to run all of my service requests on a background thread. To do this, I make a ExecutorService and redirect all calls to the main handler (handleMessage) to a handler on the new thread (handleMessageHelper):
ExecutorService background = Executors.newSingleThreadExecutor();
//handles messages from client
class IncomingHandler extends Handler {
#Override
public void handleMessage(final Message msg) {
background.execute(new Runnable() {
#Override
public void run() {
handleMessageHelper(msg); //actually handles the request
}
});
super.handleMessage(msg);
}
}
However, somehow the msg is getting corrupted between when the new Runnable is created, and when the run() function is called. Any way to prevent this?

Toast created in an IntentService never goes away

I have an IntentService that downloads some files. The problem is that I create a Toast inside the IntentService like this
Toast.makeText(getApplicationContext(), "some message", Toast.LENGTH_SHORT).show();
The Toast will never disappear event if I exit the app. The only way to destroy it is to kill the process.
What am I doing wrong?
The problem is that IntentService is not running on the main application thread. you need to obtain a Handler for the main thread (in onCreate()) and post the Toast to it as a Runnable.
the following code should do the trick:
#Override
public void onCreate() {
super.onCreate();
mHandler = new Handler();
}
#Override
protected void onHandleIntent(Intent intent) {
mHandler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(MyIntentService.this, "Hello Toast!", Toast.LENGTH_LONG).show();
}
});
}
This works for me:
public void ShowToastInIntentService(final String sText) {
final Context MyContext = this;
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
Toast toast1 = Toast.makeText(MyContext, sText, Toast.LENGTH_LONG);
toast1.show();
}
});
};
IntentService will create a thread to handle the new intent, and terminated it immediately once the task has done. So, the Toast will be out of controlled by a dead thread.
You should see some exceptions in the console when the toast showing on the screen.
For people developing in Xamarin studio, this is how its done there:
Handler handler = new Handler ();
handler.Post (() => {
Toast.MakeText (_Context, "Your text here.", ToastLength.Short).Show ();
});
To show a toast when the user is in one of the application activity.
Just need a reference of the current activity, and call it with this sample code:
public void showToast(final String msg) {
final Activity a = currentActivity;
if (a != null ) {
a.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(a, msg, Toast.LENGTH_SHORT).show();
}
});
}
}
There is a lot of options to get the current activity, check this question:
How to get current foreground activity context in android?
But I use this approach:
The application must have:
private Activity currentActivity = null;
public Activity getCurrentActivity() {
return currentActivity;
}
public void setCurrentActivity(Activity mCurrentActivity) {
this.currentActivity = mCurrentActivity;
}
Each activity must have:
#Override
protected void onResume() {
super.onResume();
((MyApplication) getApplication()).setCurrentActivity(this);
}
#Override
protected void onPause() {
super.onPause();
((MyApplication) getApplication()).setCurrentActivity(null);
}
You shouldn't create Toasts from a Service. You should use a Notification instead.

How do you display a Toast from a background thread on Android?

How can I display Toast messages from a thread?
You can do it by calling an Activity's runOnUiThread method from your thread:
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
}
});
I like to have a method in my activity called showToast which I can call from anywhere...
public void showToast(final String toast)
{
runOnUiThread(() -> Toast.makeText(MyActivity.this, toast, Toast.LENGTH_SHORT).show());
}
I then most frequently call it from within MyActivity on any thread like this...
showToast(getString(R.string.MyMessage));
This is similar to other answers, however updated for new available apis and much cleaner. Also, does not assume you're in an Activity Context.
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();
}
});
}
}
One approach that works from pretty much anywhere, including from places where you don't have an Activity or View, is to grab a Handler to the main thread and show the toast:
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.LENGTH_LONG).show();
}
});
}
The advantage of this approach is that it works with any Context, including Service and Application.
Like this or this, with a Runnable that shows the Toast.
Namely,
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();
}
Sometimes, you have to send message from another Thread to UI thread. This type of scenario occurs when you can't execute Network/IO operations on UI thread.
Below example handles that scenario.
You have UI Thread
You have to start IO operation and hence you can't run Runnable on UI thread. So post your Runnable to handler on HandlerThread
Get the result from Runnable and send it back to UI thread and show a Toast message.
Solution:
Create a HandlerThread and start it
Create a Handler with Looper from HandlerThread:requestHandler
Create a Handler with Looper from Main Thread: responseHandler and override handleMessage method
post a Runnable task on requestHandler
Inside Runnable task, call sendMessage on responseHandler
This sendMessage result invocation of handleMessage in responseHandler.
Get attributes from the Message and process it, update UI
Sample code:
/* 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);
}
Useful articles:
handlerthreads-and-why-you-should-be-using-them-in-your-android-apps
android-looper-handler-handlerthread-i
Get UI Thread Handler instance and use handler.sendMessage();
Call post() method handler.post();
runOnUiThread()
view.post()
You can use Looper to send Toast message. Go through this link for more details.
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();
}
and it is called in your thread. Context may be Activity.getContext() getting from the Activity you have to show the toast.
I made this approach based on mjaggard answer:
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();
}
});
}
Worked well for me.
Kotlin Code with runOnUiThread
runOnUiThread(
object : Runnable {
override fun run() {
Toast.makeText(applicationContext, "Calling from runOnUiThread()", Toast.LENGTH_SHORT)
}
}
)
I encountered the same problem:
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.
Before: onCreate function
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
Toast.makeText(getBaseContext(), "Thread", Toast.LENGTH_LONG).show();
}
});
thread.start();
After: onCreate function
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getBaseContext(), "Thread", Toast.LENGTH_LONG).show();
}
});
it worked.
java 11:
var handler = new Handler(Looper.getMainLooper);
handler.post(() -> Toast.makeText(your_context, "Hi!", Toast.LENGTH_SHORT).show());
Lambdas are available in java 8 though. var is introduced in java 11.
Contrary to almost every answer here, Toast#makeText and Toast#show do NOT have to run on the UI thread. The only requirement is that it runs on a thread that has called Looper#prepare.
The reasons for this is because toasts are handled and rendered by the OS, not the application. Internally, Toast#show makes a call to a system service to enqueue the toast.
This means the following code is valid
private static class MyThread extends Thread {
public Handler handler;
#Override
public void run() {
Looper.prepare();
handler = new Handler(Looper.myLooper()) {
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
Looper.loop()
}
}
final private MyThread t = new MyThread();
// start and wait for t to start looping
private void onClick() {
t.handler.post(() -> Toast.makeText(this, "this works", Toast.LENGTH_SHORT).show());
}
Method in onCreate :
private void toastPublic(final String message){
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast.makeText(getBaseContext(),""+message,
4 /*Toast.LENGTH_SHORT*/).show();
}});
}
Next : use in inside Thread

Categories

Resources