当网络不可用或已禁用时,我的应用程序未显示Toast消息?我在下面插入了我的代码,它似乎没有检查是否有可用的连接。请帮助我在网络可用时工作正常但是当我禁用我的网络时它没有显示错误消息,没有其他网络可用。
if (position == 2)
{
final ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connMgr
.getActiveNetworkInfo();
android.net.NetworkInfo mobile1 =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (activeNetworkInfo == null) {
Toast.makeText(LoginScreen.this, "No
Active Network",Toast.LENGTH_LONG).show();
} else {
if (activeNetworkInfo.isConnected()) {
btnLogin.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
String pinemptycheck = pin.getText().toString();
String mobileemptycheck = mobile.getText().toString();
if (pinemptycheck.trim().equals("")||
(mobileemptycheck.trim().equals("")))
{
Toast.makeText(getApplicationContext(), "Please Enter Correct Information",
Toast.LENGTH_LONG).show();
}
else
{
showProgress();
postLoginData();
}
}
});
} else if
(activeNetworkInfo.isConnectedOrConnecting()) {
Toast.makeText(LoginScreen.this, "network is Connecting", Toast.LENGTH_LONG)
.show();
} else if (mobile1.isAvailable()) {
btnLogin.setOnClickListener(new
OnClickListener() {
public void onClick(View view) {
showProgress();
postLoginData();
}
});
} else if (!mobile1.isAvailable()) {
Toast.makeText(LoginScreen.this,"No other Connection Found
",Toast.LENGTH_LONG).show();
btnLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Toast.makeText(LoginScreen.this," No other Connection Found", Toast.LENGTH_LONG).show();
}
});
}}}
答案 0 :(得分:0)
问题是你应该检查onClickListener中的连接可用性,而不是onClickListener。
原因在于,假设当onCreate调用时网络存在,所以它始终为true。所以当你点击按钮时检查连接是否可用。