Android: Show toast after finishing application / activity

后端 未结 2 1085
醉酒成梦
醉酒成梦 2021-01-18 10:12

I want to show a simple toast, when exiting an application. The problem is, that the toast is not shown. I assume it is because the acitivity is finished or because of

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-18 10:59

    I just fired off a new thread to allow time for the Toast to show before the system process is killed. Check it out:

    private Runnable checkForAdBlockRun = new Runnable() {
        @Override
        public void run() {
            boolean blocked = false;
            try {
                blocked = AdBlockUtil.areAdsBlocked();
                if (blocked) {
                    Log.w(TAG, "Ads are blocked on this device.");
                    adBlockHandler.sendEmptyMessage(0);
    
                }
            }
            catch (Exception e) {
                Log.w(TAG, "Could not check for ad blocking", e);
            }
        }
    };
    
    private Handler adBlockHandler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            Toast.makeText(instance, "Can not run this app with adblock on", Toast.LENGTH_LONG).show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    }
                    catch (Exception e) { }
                    System.exit(0);
                }
            }).start();
        }
    };
    

提交回复
热议问题