android连接检查

时间:2012-05-17 09:23:58

标签: android

我目前正在做一个检查网络可用性(wifi和3G)的Android应用程序。 我有一个完美的活动代码。但我需要它在广播接收器。我希望在网络可用时进行一些操作。 我有权限  

请帮助我......

代码如下......

 ConnectivityManager cm =   (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

 if ( cm.equals(ConnectivityManager.EXTRA_NO_CONNECTIVITY))
    {

  Log.v("CONNECT", "CONNECTED");
    }
 else
 {
  Log.v("CONNECT", "NOT CONNECTED"); 

 }

`

3 个答案:

答案 0 :(得分:1)

如果你声明了一个BroadcastReceiver类:

public class NetConnectionBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        final ConnectivityManager cm = (ConnectivityManager) 
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo == null)
            Log.v("CONNECT", "NOT CONNECTED");
        int netType = networkInfo.getType();
        if (netType == ConnectivityManager.TYPE_WIFI)
        {
            Log.v("CONNECT", "CONNECTED TO WIFI");
        }
        else if (netType == ConnectivityManager.TYPE_MOBILE)
        {
            Log.v("CONNECT", "CONNECTED TO MOBILE" + 
                    (networkInfo.isRoaming() ? " ROAMING!" : ""));
        }
    }
}

您可以在活动中存储它的实例,并且在onCreate方法中,您可以注册android.net.conn.CONNECTIVITY_CHANGE事件:

private final  NetConnectionBroadcastReceiver receiver = 
        new NetConnectionBroadcastReceiver();
public void onCreate(Bundle savedInstanceState)
{
    // ... your onCreate implementation goes here 
    final IntentFilter intentFilter = 
        new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
    this.registerReceiver(this.receiver, intentFilter);
}

当数据连接发生变化时,您会调用接收方的onReceive方法。你可以从内部检查发生了什么样的变化。

对于此检查,您不需要超过android.permission.ACCESS_NETWORK_STATE权限。

答案 1 :(得分:1)

您应该在连接状态发生变化时触发BroadcastReceiver

public class BroadCastSampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.registerReceiver(this.mConnReceiver,
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }
    private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
            boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

            NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

            if(currentNetworkInfo.isConnected()){
                Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
            }
        }
    };
}

然后在您的AndroidManifest中,您可以检查您是否有连接:

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

有关更多源代码Download

答案 2 :(得分:-1)

private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }