0
目前我的應用程序崩潰,如果沒有啓用互聯網。我想知道如何捕捉異常,並顯示我的popupdialog(下面),以便他們可以導航以重新打開數據。我檢查這樣的電話狀態:Android:捕捉異常,如果移動數據未啓用
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()的開頭調用函數。
在此先感謝!
謝謝,我沒有使用你的代碼,但你的想法。我在檢查中使用互聯網包裹了我的功能,它效果很好!再次感謝。 – Nick