0

我正在尝试查看应用程序启动时用户是否有互联网连接:

package nco.supp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import nco.supp.Status;
import nco.supp.Register;

public class ncosuppActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        NetworkManager networkManager = new NetworkManager();
        if (!networkManager.haveNetworkConnection()) {

            AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
            dlgAlert.setPositiveButton("Ok", null);

        }

        Button loginButton = (Button) findViewById(R.id.loginButton);
        loginButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(ncosuppActivity.this, Status.class));

            }
        });

        Button registerButton = (Button) findViewById(R.id.registerButton);
        registerButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(ncosuppActivity.this, Register.class));

            }
        });
    }
}

package nco.supp;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkManager extends Activity{


    public boolean haveNetworkConnection() {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }


}

我收到此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{nco.supp/nco.supp.ncosuppActivity}:
java.lang.IllegalStateException: System services not available to Activities before onCreate()

有任何想法吗??

4

1 回答 1

1

您试图在Activity没有该方法的类中创建一个方法onCreate(),这是错误的。MainActivity只需在您的as 中创建一个方法

private boolean checkConnectivity() {
        ConnectivityManager manager = (ConnectivityManager) 
                             getSystemService(Context.CONNECTIVITY_SERVICE);
        return manager.getActiveNetworkInfo() != null && manager.
                             getActiveNetworkInfo().isConnectedOrConnecting();
    }

这应该让它工作。

于 2012-06-08T10:46:32.243 回答