0

我修改了我的最后一个代码,但此代码仍然显示找到网络

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();
    }

    });
    }} 
4

2 回答 2

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();

                }
于 2012-10-02T13:15:46.550 回答
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();

}
        }

}

于 2012-10-02T13:25:10.160 回答