在Android安装应用程序后检查互联网连接

时间:2014-03-24 11:44:37

标签: android internet-connection

我想以这样的方式设计我的应用程序:当应用程序安装在设备中时,应首先检查应用程序是否已连接到互联网,然后再显示布局。所以关于下面的这种方法。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    connectivity = new ConnectionDetector(getApplicationContext());
    alertDialog =  new AlertDialogManager();
    //Checking for Internet connection
    if(!connectivity.isConnectingToInternet()){
        alertDialog.showAlertDialog(getApplicationContext(), "Internet Connection Error", "Not connected to internet", false);
        return ;
    }
    else{
        setContentView(R.layout.activity_register);

        me = (EditText) findViewById(R.id.fname);
        email = (EditText) findViewById(R.id.email);
        age = (EditText) findViewById(R.id.age);
        address = (EditText) findViewById(R.id.address);
        state = (EditText) findViewById(R.id.state);
        country = (EditText) findViewById(R.id.country);
        done = (Button) findViewById(R.id.done);
    }
}

我的ConnectionDector类如下:

public class ConnectionDetector {

private Context context;

public ConnectionDetector(Context context){
    this.context = context;
}

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] info = connectivity.getAllNetworkInfo();
    if(info != null){
        for(int i = 0; i< info.length; i++){
            if(info[i].getState() == NetworkInfo.State.CONNECTED){
                return true;
            }
        }
    }
    return false;
}
}

以下是我的AlertDialogManager类:

    public class AlertDialogManager {

@SuppressWarnings("deprecation")
 public void showAlertDialog(Context context, String title, String message, Boolean status){
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle(title);
    alertDialog.setMessage(message);
    if(status != null){
        alertDialog.setIcon((status)?R.drawable.success : R.drawable.fail);
    }
    alertDialog.setButton("Okay", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    alertDialog.show();
}

如果我连接到wifi,我的应用程序将正确安装。如果我尝试安装它时它不会崩溃并发出以下错误:

03-24 17:01:11.169: E/AndroidRuntime(6101): FATAL EXCEPTION: main
03-24 17:01:11.169: E/AndroidRuntime(6101): Process: com.iriemo.safetyapp, PID: 6101
03-24 17:01:11.169: E/AndroidRuntime(6101): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.example.demoApp/com.example.demoApp.RegisterActivity}: java.lang.NullPointerException
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.os.Looper.loop(Looper.java:136)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at java.lang.reflect.Method.invoke(Method.java:515)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at dalvik.system.NativeStart.main(Native Method)
03-24 17:01:11.169: E/AndroidRuntime(6101): Caused by: java.lang.NullPointerException
03-24 17:01:11.169: E/AndroidRuntime(6101):     at  RegisterActivity.onCreate(RegisterActivity.java:26)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at      android.app.Activity.performCreate(Activity.java:5231)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-24 17:01:11.169: E/AndroidRuntime(6101):     ... 11 more

2 个答案:

答案 0 :(得分:1)

添加alertDialog = new AlertDialog(getApplicationContext());这将解决null pointer exception

答案 1 :(得分:0)

改变这一点。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
connectivity = new ConnectionDetector(this);

//Checking for Internet connection
if(!connectivity.isConnectingToInternet()){
    alertDialog.showAlertDialog(getApplicationContext(), "Internet Connection Error", "Not connected to internet", false);
    return ;
}
else{
   // Something else.  
}

me = (EditText) findViewById(R.id.fname);
email = (EditText) findViewById(R.id.email);
age = (EditText) findViewById(R.id.age);
address = (EditText) findViewById(R.id.address);
state = (EditText) findViewById(R.id.state);
country = (EditText) findViewById(R.id.country);
done = (Button) findViewById(R.id.done);