如何在Android中取消吐司?

时间:2014-08-08 15:30:30

标签: android xml android-layout

public class MyAccessibilityService extends AccessibilityService {

    public static final String TAG = "volumeMaster";
    Context cx;
    String evt;
    public FileUtils fileUtils = new FileUtils();
    Toast toast = null;


    public PrijectorLauncher pm = null;@Override
    public void onAccessibilityEvent(AccessibilityEvent event) {


        evt = "getEventType " + event.getEventType() + "\n getWindowId " + event.getWindowId() + "\n getPackageName " + event.getPackageName() + "\n getBeforeText " + event.getBeforeText() + "\n getEventTime " + event.getEventTime() + "\n toString " + event.toString();

        Log.d(TAG, evt);
        LayoutInflater inflater = (LayoutInflater) cx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        View layout = inflater.inflate(R.layout.toast, null);
        View toastlayout = inflater.inflate(R.layout.toast, (ViewGroup) layout.findViewById(R.id.toast_layout_root));
        WebView webview = (WebView) layout.findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setAllowFileAccess(true);
        webview.getSettings().setPluginsEnabled(true);
        webview.getSettings().setPluginState(PluginState.ON);
        webview.getSettings().setUserAgentString("iPad");
        webview.getSettings().setPluginsPath("/data/data/com.adobe.flashplayer/lib");
        webview.getSettings().setSupportZoom(false);
        webview.getSettings().setBuiltInZoomControls(false);
        webview.refreshDrawableState();
        webview.getSettings().setSaveFormData(true);
        webview.getSettings().setSavePassword(true);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        webview.getSettings().setAppCachePath("/data/data/" + cx.getPackageName() + "/cache/");
        webview.getSettings().setDatabasePath("/data/data/" + cx.getPackageName() + "/databases/");
        webview.getSettings().setDatabaseEnabled(true);
        webview.getSettings().setAppCacheEnabled(true);
        webview.setWebViewClient(new WebViewClient());
        webview.setWebChromeClient(new WebChromeClient());
        webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setRenderPriority(RenderPriority.HIGH);
        webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        webview.getSettings().setUseWideViewPort(false);
        webview.getSettings().setPluginState(PluginState.ON_DEMAND);

        webview.setPadding(0, 0, 0, 0);
        webview.setInitialScale(100);

        webview.addJavascriptInterface(this, "PRIVYTV");
        webview.loadUrl("file:///mnt/sdcard/prijector/pages/accessibility.html");


        toast = new Toast(getApplicationContext());


        if (event.getPackageName().equals("com.privytv.prijectorlauncher")) {
            Log.v(TAG, "*****Hiding onAccessibilityEvent");
            // Toast.makeText(this, "", Toast.LENGTH_LONG);

            return;
        } else {
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(60000);
            //toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(toastlayout);
            toast.setGravity(Gravity.BOTTOM, 0, 0);
            Log.v(TAG, "***** onAccessibilityEvent");
            toast.show();
        }

    }



    public FileUtils getFileUtils() {

        return fileUtils;

    }

    private class MyWebViewClient extends WebViewClient {@Override
        public void onReceivedHttpAuthRequest(WebView view,
        HttpAuthHandler handler, String host, String realm) {

            Log.d("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", "WEBVIEW onReceivedHttpAuthRequest : " + host + " " + realm);
            //   handler.proceed("me@test.com", "mypassword");
            handler.proceed("prijector", "android");

        }
    }

    public class MyJavaScriptInterface {

        public String getfilelist(String channel) {
            return "BABABABABABABA " + channel;
        }

        public void playSound(String path) {}

        public void showToast(String toast) {
            Log.i(TAG, "DATA FROM JAVASCRIPT SHOTOAST" + toast);
        }

        public void log(String a) {
            Log.i(TAG, "JAVASCRIPT" + a);
        }

        public void loaded(String a) {
            Log.i(TAG, "JAVASCRIPT: WEBVIEW LOADED" + a);
        }

        public void finished(String a) {}
    }

    WebChromeClient chrome = new WebChromeClient() {

        public void onShowCustomView(View view, CustomViewCallback callback) {
            super.onShowCustomView(view, callback);
        }

        public void onReceivedHttpAuthRequest(WebView view,
        HttpAuthHandler handler, String host, String realm) {

            Log.d("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", "WEBVIEW onReceivedHttpAuthRequest : " + host + " " + realm);
            handler.proceed("prijector", "android");

        }

        public void onProgressChanged(WebView view, int progress) {}

        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            Log.d(TAG, "WEBVIEW" + consoleMessage.lineNumber() + ":  " + consoleMessage.message());
            return true;
        }

        public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
            Log.d(TAG, "WEBVIEW" + message);
            return true;
        };

    };

    @Override
    public void onInterrupt() {
        Log.v(TAG, "***** onInterrupt");
    }



    @Override
    public void onServiceConnected() {
        Log.v(TAG, "***** onServiceConnected");

        cx = getApplicationContext();
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.eventTypes = AccessibilityEvent.TYPE_VIEW_FOCUSED;

        info.notificationTimeout = 1000;
        info.feedbackType = AccessibilityEvent.TYPES_ALL_MASK;
        setServiceInfo(info);

    }




}

我正在尝试取消某个特定包的吐司,但它不起作用

任何人都可以建议我,如何实现这一目标?

如果我们在某个特定时间显示任何吐司,是否可以隐藏在其间?

2 个答案:

答案 0 :(得分:0)

Toast.makeText返回Toast对象。在此对象上调用cancel()以取消它。

答案 1 :(得分:0)

根据我的经验,大多数情况下调用Toast的cancel()方法都没有效果,您可以自定义一个类来管理您的项目的吐司,如下所示:

public class ToastUtil {

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

private static Toast toast = null;

private static Object synObj = new Object();

public static void showMessage(final Context act, final String msg) {
    showMessage(act, msg, Toast.LENGTH_SHORT);
}

public static void showMessage(final Context act, final int msg) {
    showMessage(act, msg, Toast.LENGTH_SHORT);
}

public static void showMessage(final Context act, final String msg,
        final int len) {
    new Thread(new Runnable() {
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    synchronized (synObj) {
                        if (toast != null) {
                            toast.cancel();
                            toast.setText(msg);
                            toast.setDuration(len);
                        } else {
                            toast = Toast.makeText(act, msg, len);
                        }
                        toast.show();
                    }
                }
            });
        }
    }).start();
}


public static void showMessage(final Context act, final int msg,
        final int len) {
    new Thread(new Runnable() {
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    synchronized (synObj) {
                        if (toast != null) {
                            toast.cancel();
                            toast.setText(msg);
                            toast.setDuration(len);
                        } else {
                            toast = Toast.makeText(act, msg, len);
                        }
                        toast.show();
                    }
                }
            });
        }
    }).start();
}}