Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

im working on an android application in which i have a main activity and Activity_splash.xml which is my splash screen activity. what i want is when my app is launched, and if there is internet connection, the splash screen should appear for 3 seconds and after that main activity should launch and if there is No internet turned on, then it should show "No internet connection " toast and open wireless networks settings and after enabling wifi, if return button is pressed, user should be brought back to splash screen... hope this is understandable.. but my code is not working as im getting bunch of errors which are

1.ACTION_WIRELESS_SETTINGS cannot be resolved or is not a field.
2.syntax error on token "{",{ expected after this token (in line public class SplashScreen extends Activity {) 3.syntax error on token "(",; expected and syntax error on token ")",; expected (in line protected 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);
    }
  }
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

try this

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...