0

I have a splashscreen (for try Internet), if we can't connect, we don't launch app.

So, i have try to found one java class on internet and i have found that :https://stackoverflow.com/a/6987498/5070495

I have adapt the code, but i have an error in

if (SplashScreen.getInstance(this).isOnline()) {

Error :

getInstance(android.content.Context')in 'com.srazzz.package.Splashscreen' cannot be applied to '(anonymous java.lang.thread')

All my class

public class SplashScreen extends Activity {
    static Context context;
    ConnectivityManager connectivityManager;
    NetworkInfo wifiInfo, mobileInfo;
    boolean connected = false;
    private static SplashScreen instance = new SplashScreen();

    public static SplashScreen getInstance(Context ctx) {
        context = ctx.getApplicationContext();
        return instance;
    }

    public boolean isOnline() {
        try {
            connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            connected = networkInfo != null && networkInfo.isAvailable() &&
                    networkInfo.isConnected();
            return connected;


        } catch (Exception e) {
            System.out.println("CheckConnectivity Exception: " + e.getMessage());
            Log.v("connectivity", e.toString());
        }
        return connected;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Thread timerThread = new Thread(){
            public void run(){
                try{
                    sleep(3000);

                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{

                    if (SplashScreen.getInstance(this).isOnline()) {

                        Intent i = new Intent(SplashScreen.this, MainActivity.class);
                        startActivity(i);

                    } else {

                        //Toast t = Toast.makeText("test").show();
                        //Log.v("Home", "############################You are not online!!!!");
                        Intent i = new Intent(SplashScreen.this, chatonly.class);
                        startActivity(i);
                    }

                }
            }
        };
        timerThread.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }



}
Community
  • 1
  • 1
Argardor
  • 79
  • 1
  • 2
  • 11

2 Answers2

1

Use like this,

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

            @Override
            public void run() {
                if (isOnline()) {    
                        Intent i = new Intent(SplashScreen.this, MainActivity.class);
                        startActivity(i);

                    } else {

                        //Toast t = Toast.makeText("test").show();
                        //Log.v("Home", "############################You are not online!!!!");
                        Intent i = new Intent(SplashScreen.this, chatonly.class);
                        startActivity(i);
                    }
            }
        }, 3000);
Krishna V
  • 1,801
  • 1
  • 14
  • 16
  • Instead of `if (SplashScreen.getInstance(this).isOnline()) {`, you can just do `if (isOnline()) {`. `instance` and the `getInstance(Context ctx)` can then be removed from the OP's code as they are pointless. – HexAndBugs Jul 07 '15 at 19:46
  • Oh fine ! Thank you <3 – Argardor Jul 07 '15 at 19:51
0

Try writing out fully qualified thread class name as example

new java.lang.Thread(new Runnable() {

public void run() {   

       if (SplashScreen.getInstance(this).isOnline()) {
//do stuff here} else{
//Don't do stuff}     }
 }).start();