当我运行此应用程序时,如果没有Internet连接,它会崩溃 为什么呢?
我有一个单独的类,我有一个提供Internet详细信息的方法 Internet清单中设置了Internet权限
public class MainActivity extends Activity {
private WebView webView;
private AlertDialog.Builder alertDiaBuilder;
private ProgressDialog progressBar;
private int netState = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
progressBar = new ProgressDialog(MainActivity.this);
progressBar.setCancelable(false);
progressBar.setTitle("Checking network connectivity");
progressBar.setMessage("Wait for a movement!");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.show();
new Thread(new Runnable() {
public void run() {
netState = NetworkUtils.getConnectivityStatus(MainActivity.this);
if (netState == 1 || netState == 2 || netState == 3
|| netState == 4) {
try {
Thread.sleep(5000);
progressBar.dismiss();
webView.loadUrl("http://www.yahoo.co.in");
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
alertDiaBuilder = new AlertDialog.Builder(MainActivity.this);
alertDiaBuilder.setMessage("Not Connected to Internet");
alertDiaBuilder.setCancelable(false);
alertDiaBuilder.setNegativeButton("Close application",
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
finish();
//dialog.cancel();
}
});
alertDiaBuilder.setPositiveButton("Go offline",
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(getApplicationContext(),
"Ready to go offline",
Toast.LENGTH_LONG).show();
}
});
AlertDialog alertDialog = alertDiaBuilder.create();
alertDialog.show();
}
}
}).start();
}
}