在Android Service组件里面弹出Toast

    /**
     * 在Service里面弹出Toast
     * 
     * @param txt 需要显示的文字
     */
    public void showToast(final String txt) {
        if (txt == null) {
            Log.e(TAG, "call method showToast, text is null.");
            return;
        }

        Handler handler = new Handler(Looper.getMainLooper());

        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(DeviceServiceEngineImpl.this, txt, Toast.LENGTH_LONG)
                        .show();
            }
        });
    }

你可能感兴趣的:(代码笔记)