Android:完成应用程序/活动后显示吐司

发布于 2024-09-19 03:19:04 字数 390 浏览 6 评论 0原文

我想在退出应用程序时显示一个简单的吐司。问题是,吐司没有显示。我认为这是因为活动已完成或因为 System.exit(0),但我不知道如何解决它。有人有提示吗?谢谢!!

在我的活动中,我有以下代码:

Toast.makeText(this,"Exit application.",Toast.LENGTH_SHORT).show();
exitApp();

public void exitApp (){
  App.getInstance().exit();
  finish();
}

并且该方法在应用程序中退出:

public void exit() {
   System.exit(0);
}

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 System.exit(0), but I don't know how to solve it. Does anyone have a tip? Thanks!!

In my activity I have the following code:

Toast.makeText(this,"Exit application.",Toast.LENGTH_SHORT).show();
exitApp();

public void exitApp (){
  App.getInstance().exit();
  finish();
}

And the mehod exit in App:

public void exit() {
   System.exit(0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧话新听 2024-09-26 03:19:04

建议您调用 finish 来关闭应用程序,而不是调用 System.exit(0);,因为这种方法会完全终止您的应用程序。System.exit() 杀死你的整个进程。
finish() 只是隐藏、停止并销毁您的活动。您的进程仍在运行。

您只需使用 finish(); 关闭您的活动,这应该可以解决您的问题。

http://groups.google.com/group/ android-developers/browse_thread/thread/63de8a9cdffa46a3?pli=1

It is advisable that you call finish to close your application rather than calling System.exit(0); since this approach will kill your application completely.System.exit() kills your entire process.
finish() just hides, stops and destroys your activity. Your process is still running.

You can just use finish(); to close your activity and this should solve your problem.

http://groups.google.com/group/android-developers/browse_thread/thread/63de8a9cdffa46a3?pli=1

一抹苦笑 2024-09-26 03:19:04

我刚刚启动了一个新线程,以便在系统进程被终止之前让 Toast 有时间显示。一探究竟:

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();
    }
};

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();
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文