2019年1月13日 星期日

How to pop a toast message in Service Android



Step1. define function

    public void sentToToast(final String content,final  int type,Handler handler){
        handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),content,type).show();
            }
        });
    }

Step2. call the function

Handler handler;
String msg = "12345678";
sentToToast(msg,Toast.LENGTH_LONG,handler);


1 則留言: