如何检测互联网和打开无线设置

时间:2013-11-22 03:26:05

标签: android

这是我的应用程序应该如何工作http://postimg.org/image/57byitwpz/ 这是我得到的错误http://postimg.org/image/ynyqerln9/ 请不要在我的代码中添加任何内容,而是更正我的代码。 我错误地在第一张图片中写了.xml而不是.class,请忽略

 here is my code

 **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
     // this is my code of splash screen
    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);
    }





}

2 个答案:

答案 0 :(得分:2)

试试这可能会对你有所帮助

import android.app.Activity;
  import android.app.AlertDialog;
  import android.app.AlertDialog.Builder;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.DialogInterface.OnClickListener;
  import android.content.Intent;
  import android.net.ConnectivityManager;
  import android.os.Bundle;
  import android.provider.Settings;
  import android.util.Log;
  import android.widget.Toast;

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);// checking
    // internet
    build = new Builder(Splash.this); // connectivity
    setContentView(R.layout.activity_splash);
    if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)// if connection is
            // there screen goes
            // to next screen
            // else shows
            // message
            .isConnectedOrConnecting()
            || cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                    .isConnectedOrConnecting()) {
        Log.e("cm value",
                ""
                        + cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                                .isConnectedOrConnecting());
        Toast.makeText(Splash.this, "Internet is active", 2000).show();
        Thread mythread = new Thread() {
            public void run() {
                try {

                    sleep(5000);

                } catch (Exception e) {
                } finally {
                    Intent intent = new Intent(Splash.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

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

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

    }

}

}

答案 1 :(得分:0)

互联网连接检查代码应在您活动的onCreate()方法中完成。

请检查:Detect internet and wireless sttings

希望这有帮助。