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

ε祈祈猫儿з 提交于 2019-11-28 20:44:55
kalyan pvs

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.

Little Child

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));
TechHelper

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();
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!