1

I am currently doing an android application that checks network availability(both wifi & 3G). i have a code that perfectly working in activity . but i need it in broadcast receiver . i want to do some operations when the network is available . i have the permission

plz help me.....

The code is given below...

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

 }

`

4

3 回答 3

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权限即可。

于 2012-05-17T09:41:02.367 回答
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" />

更多源代码下载

于 2012-05-17T09:41:30.267 回答
-1
private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }
于 2012-05-17T09:55:04.763 回答