В настоящее время мое приложение падает, если не включен интернет. Я хотел знать, как перехватить исключение, и показать мой всплывающий диалог (см. Ниже), чтобы они могли затем перемещаться, чтобы снова включить данные. Я проверяю состояние телефона следующим образом:
public void CheckInternet()
{
ConnectivityManager connec = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// Here if condition check for wifi and mobile network is available or not.
// If anyone of them is available or connected then it will return true, otherwise false;
if (wifi.isConnected()) {
} else if (!mobile.isConnected()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You need to enable mobile data in order to use this application:")
.setCancelable(false)
.setPositiveButton("Turn on Data", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Intent newintent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivity(newintent);
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Main.this.finish();
}
});
AlertDialog alert = builder.show();
} else if (mobile.isConnected()) {
//nothing
}
}
И я вызываю функцию в начале onCreate ().
Заранее спасибо!