在 Android 启动画面上显示 Toast

发布于 2024-12-07 08:08:31 字数 2274 浏览 1 评论 0原文

我遇到了以下问题。如果检测到没有网络连接,我希望我的应用程序退出。我的应用程序以启动屏幕启动。如果设备上没有网络连接,是否可以显示启动屏幕,然后显示烤面包。然后终止应用程序

我的启动屏幕代码中有类似这样的内容:

在 onCreate()

ConnectivityManager connectivitymanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
        connected = networkinfo != null && networkinfo.isAvailable()
                && networkinfo.isConnected();
        Log.v("Network state : ", connected + "");

        Thread splashThread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while (waited < 5000) {
                        sleep(100);
                        waited += 100;
                    }
                } catch (InterruptedException e) {
                    // do nothing
                } finally {
                    Looper.prepare();
                    if (connected == false) {
                        Toast.makeText(
                                splashscreenActivity.this,
                                "No Network Connection is available on device.",
                                Toast.LENGTH_LONG).show();
                        finish();
                        System.exit(0);
                    } else {
                        finish();
                        startActivity(new Intent(splashscreenActivity.this,
                                mainActivity.class));
                    }
                    Looper.loop();
                }
            }
        };
        splashThread.start();

1.请参阅我的代码并指导我如何显示该吐司。 2.或者建议我其他更好的方法来做到这一点

谢谢

编辑:

谢谢大家的回复:

我选择了Dharmendra通过闪屏活动显示吐司的方式:

有效的代码是:

if (connected == false) {
            splashscreenActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(splashscreenActivity.this,
                            "No Internet Connection.", 3000).show();
                }
            });
            finish();
        } else {
            //migrate to main activity from splashscreen
        }

I am stuck with following problem.I want my application to exit if it detects no network connection.My application starts with splash screen.Is it possible to show splash screen followed by toast if no network connection is on device.and then terminate the application

I have something like this in my splash screen code :

Inside onCreate()

ConnectivityManager connectivitymanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
        connected = networkinfo != null && networkinfo.isAvailable()
                && networkinfo.isConnected();
        Log.v("Network state : ", connected + "");

        Thread splashThread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while (waited < 5000) {
                        sleep(100);
                        waited += 100;
                    }
                } catch (InterruptedException e) {
                    // do nothing
                } finally {
                    Looper.prepare();
                    if (connected == false) {
                        Toast.makeText(
                                splashscreenActivity.this,
                                "No Network Connection is available on device.",
                                Toast.LENGTH_LONG).show();
                        finish();
                        System.exit(0);
                    } else {
                        finish();
                        startActivity(new Intent(splashscreenActivity.this,
                                mainActivity.class));
                    }
                    Looper.loop();
                }
            }
        };
        splashThread.start();

1.Please see my code and guide me how can i show up that toast.
2.Or suggest me some other better way to do this

Thanks

EDIT :

Thank you everybody for replying :

I opted Dharmendra's way of showing toast via splashscreen activity :

The code that worked for is :

if (connected == false) {
            splashscreenActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(splashscreenActivity.this,
                            "No Internet Connection.", 3000).show();
                }
            });
            finish();
        } else {
            //migrate to main activity from splashscreen
        }

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

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

发布评论

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

评论(4

自由范儿 2024-12-14 08:08:31

您可以这样做,使用 Handler 。将以下代码放在 else 条件中

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

mHandler.post(new Runnable() {
                                public void run() {

                                    Toast.makeText(Splash.this, "Network error", Toast.LENGTH_LONG).show(); 
                                      finish();

                                }
                            });

或者,我在我的应用程序中执行此操作:

if(status.equals("CONNECTED"))
                        {
                        startActivity(new Intent(Splash.this,Activity.class));
                        finish();
                        }else
                        { 
                        startActivity(new Intent(Splash.this,NetworkError.class));
                        finish();
                        }

其中 NetworkError 类显示另一个布局,其图像如“无网络、、、、”(或您想要的任何内容)想要展示而不是泼溅...)

You can do it like this, use Handler .Put the following code in the else condition

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

mHandler.post(new Runnable() {
                                public void run() {

                                    Toast.makeText(Splash.this, "Network error", Toast.LENGTH_LONG).show(); 
                                      finish();

                                }
                            });

Or ,I am doing this in my application :

if(status.equals("CONNECTED"))
                        {
                        startActivity(new Intent(Splash.this,Activity.class));
                        finish();
                        }else
                        { 
                        startActivity(new Intent(Splash.this,NetworkError.class));
                        finish();
                        }

where NetworkError class shows another layout with the image like "No Network,,,,"(or whatever you want to show instead of splash...)

迷鸟归林 2024-12-14 08:08:31

您正在从线程创建并显示 Toast,因此可能不会调用它

您必须编写此代码

splashscreenActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(c, "Internet connection not currently available.", 3000).show();
                }
            });

You are creating and showing Toast from Thread so it may be not called

You have to write this code

splashscreenActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(c, "Internet connection not currently available.", 3000).show();
                }
            });
偏爱自由 2024-12-14 08:08:31

只需添加一个 if else 块。(使用您的连接网络信息)

if(connected){
//put the splash thread here
}else{
 finish();
}

Just add a if else block.(using ur connection network info)

if(connected){
//put the splash thread here
}else{
 finish();
}
零度℉ 2024-12-14 08:08:31

如果您不希望捕获任何异常,则可以使用finally 块。在您的代码中,将所有代码放在 else 循环中,如果时间经过 5000,则转到 else 语句并执行它。如果你要添加catch,你可以最后删除。

A finally block is used if you do not wish to catch any exceptions. In your code, place all your code in a else loop, If time of 5000 lapses then, go to else statement and execute it. If you are adding catch you can remove finally.

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