How can I put Toast in a Runnable of a Service?

2019-08-13 16:06发布

I have a service which contains a Timer and TimerTask for receiving data from Webservice in periods of time. everything works fine except Toast. I want to show a Toast to user in procSendMapMovements but i get exception. How can I use Toast in it?

class taskSendMapMovements extends TimerTask {
        @Override
        public void run() {
            hhSendMapMovements.sendEmptyMessage(0);
        }
    };



    // /////////////////////

    final Runnable rSendMapMovements = new Runnable()
    {
        public void run()
        {
            procSendMapMovements();
        }
    };

    final Handler hhSendMapMovements = new Handler(new Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            performOnBackgroundThread(rSendMapMovements);

            return false;
        }
    });

    // /////////////////////

    public void procSendMapMovements() {
        try {


            Toast.makeText(SrvDataExchange.this,
                    "some texts"
                    Toast.LENGTH_SHORT).show();
// exception here
// my process


        } catch (Exception e) {


        }
    }


@Override
    public void onStart(Intent intent, int startId) {
        try {



            timerSendMapMovements = new Timer();


            timerSendMapMovements
                        .schedule(new taskSendMapMovements(),
                                10*60*1000,
                                10*60*1000);

            //

        } catch (NumberFormatException e) {
            Toast.makeText(this, "error running service: " + e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this, "error running service: " + e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
    }



public static Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {

                }
            }
        };
        t.start();
        return t;
    }

2条回答
啃猪蹄的小仙女
2楼-- · 2019-08-13 16:28

Create a handler to display toast.

Use following:

where you want to display toast call:

//printToast.sendEmptyMessage();//If you dont want to send no paramter
//if you want to send some object to handler
/*
Message msg=printToast.obtainMessage();
msg.obj=objToSent;
printToast.sendMessage(msg);
*/
final Handler printToast= new Handler(new Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Toast.makeText....;

        }
    });
查看更多
来,给爷笑一个
3楼-- · 2019-08-13 16:30

**

Handler printToast= new Handler(new Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            Toast.makeText....;
        }
    });
your Runnable
{

   run()
   {

      do what ever you want 
      printToast.sendMessage(printToast.obtainMessage());
        }

}

**

查看更多
登录 后发表回答