检查我们是否已连接(WIFI / 3-4G) - Android Java

时间:2015-07-07 19:24:51

标签: java android

我有一个启动画面(尝试上网),如果我们无法连接,我们就不会启动应用。

所以,我试图在互联网上找到一个java类,我发现:https://stackoverflow.com/a/6987498/5070495

我已经调整了代码,但我在

中有错误
if (SplashScreen.getInstance(this).isOnline()) {

错误:

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

我所有的课程

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



}

2 个答案:

答案 0 :(得分:1)

像这样使用,

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

答案 1 :(得分:0)

尝试写出完全限定的线程类名称作为示例

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

public void run() {   

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