アプリの起動時にユーザーがインターネットに接続しているかどうかを確認しようとしています:
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()
何か案は??