开发者

Show Toast on SplashScreen Android

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
        }


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...)


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


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜