如果netwok被禁用,则mobiletype始终显示网络

时间:2012-10-02 12:57:47

标签: android

我修改了我的最后一段代码,但是这段代码仍然显示网络发现我打开了我的网络,但是再次显示网络显示如果我推销网络连接是stil show toast其他连接发现我不知道y请帮助我

final ConnectivityManager connMgr = (ConnectivityManager)       
  getSystemService(Context.CONNECTIVITY_SERVICE);

 final android.net.NetworkInfo mobile1 = 
                            connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);


    if (mobile1.isAvailable()) {
    Toast.makeText(LoginScreen.this," other Connection Found  
",Toast.LENGTH_LONG).show();
                   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 (!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();
    }

    });
    }} 

2 个答案:

答案 0 :(得分:1)

试试这个:

final ConnectivityManager connMgr = (ConnectivityManager)       
                  getSystemService(Context.CONNECTIVITY_SERVICE);

                 final android.net.NetworkInfo mobile1 = 
                                            connMgr.getActiveNetworkInfo();


                   if (mobile1 != null  && mobile1.isConnected() && mobile1.isAvailable() && (mobile1.getType() == ConnectivityManager.TYPE_MOBILE)) {
                Toast.makeText(CheckBoxTest.this," other Connection Found ",Toast.LENGTH_LONG).show();

                }

答案 1 :(得分:0)

您可以尝试这样的事情: -

在您的Android清单中获得以下权限 -

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在意图过滤器中使用具有以下操作的广播接收器 -

            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

在接收器的onReceive方法中使用以下代码: -

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            final ConnectivityManager connMgr = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            final android.net.NetworkInfo wifi = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            final android.net.NetworkInfo mobile = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (wifi.isConnected() || mobile.isConnected()) {
                      Toast.makeText(LoginScreen.this," other Connection Found  
",Toast.LENGTH_LONG).show();

            }
else
{
Toast.makeText(LoginScreen.this,"No other Connection Found ",Toast.LENGTH_LONG).show();

}
        }

}