如何在Splash Screen上展示吐司

时间:2013-11-21 10:59:54

标签: android

我正在开发一个Android应用程序,其中我有一个主要活动和Activity_splash.xml 这是我的启动画面活动。我想要的是当我的应用程序启动时,如果有互联网连接,启动屏幕应该出现3秒钟,然后主要活动应该启动,如果没有互联网打开,那么它应该显示“没有互联网连接”吐司和打开无线网络设置,启用wifi后,如果按下返回按钮,用户应该回到启动画面...希望这是可以理解的..但我的代码不能正常工作,因为我得到了一堆错误

1。ACTION_WIRELESS_SETTINGS无法解析或不是字段 2.令牌"{",{ expected after this token (in line public class SplashScreen extends Activity {)上的2.syntax错误 3.期望令牌"(",; expected and syntax error on token ")",;上的错误(在行保护的void onCreate(Bundle savedInstanceState){)

SplashScreen.java

public class SplashScreen extends Activity {
if (!NetworkCheckClass.haveNetworkConnection(SplashActivity.this)) {
    Toast.makeText(SplashScreen.this, "No internet connection!",             Toast.LENGTH_LONG).show();

    Intent intent=new Intent(Settings.ACTION_WIRELESS_SETTINGS);
    startActivity(intent);
}
else {
    // your code if connection is available
} // Splash screen timer
    private static int SPLASH_TIME_OUT = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable() {

            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }
  }
  }

3 个答案:

答案 0 :(得分:1)

试试这个

   public class SplashScreen extends Activity {
static ConnectivityManager cm;
AlertDialog dailog;
AlertDialog.Builder build;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    build = new Builder(Context); 

    if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .isConnectedOrConnecting()
            || cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                    .isConnectedOrConnecting()// if connection is
    // there screen goes
    // to next screen
    // else shows
    // message toast
    ) {
        Log.e("cm value", "" + cm.getAllNetworkInfo().toString());
        Toast.makeText(SplashScreen.this, "Internet is active", 2000)
                .show();
        Thread mythread = new Thread() {
            public void run() {
                try {

                    sleep(5000);

                } catch (Exception e) {
                } finally {
                    Intent intent = new Intent(SplashScreen.this,
                            Yournextactivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        };
        mythread.start();
    } else {

        build.setMessage("This application requires Internet connection.Would you connect to internet ?");
        build.setPositiveButton("Yes", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                finish();
                startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

            }
        });
        build.setNegativeButton("No", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                build.setMessage("Are sure you want to exit?");
                build.setPositiveButton("Yes", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        finish();
                    }
                });
                build.setNegativeButton("NO", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        finish();
                        Intent intent = new Intent(SplashScreen.this,
                                SplashScreen.class);
                        startActivity(intent);

                        dialog.dismiss();

                    }
                });
                dailog = build.create();
                dailog.show();
            }
        });
        dailog = build.create();
        dailog.show();

    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

答案 1 :(得分:0)

private void checknetworkavailability() {
    // TODO Auto-generated method stub

      ConnectivityManager cm =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnectedOrConnecting()) {

                // call your intent
            }
            else{
                Toast.makeText(context, "Please Check Your Internet connection", Toast.LENGTH_LONG).show();
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {

                   @Override
                public void run() {

                 //  call settings intent

                   }

                }, 3000);

            }

}

使用此方法checknetworkavailability ..

答案 2 :(得分:0)

在活动的OnCreate()中写下以下代码:

if (!NetworkCheckClass.haveNetworkConnection(SplashActivity.this)) {
Toast.makeText(SplashScreen.this, "No internet connection!",             Toast.LENGTH_LONG).show();

Intent intent=new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
}
else {
 // your code if connection is available
} // Splash screen timer