Reputation: 110540
Can you please tell me how can I detect network connectivity in android?
An example is when the airplane mode is on and off. How can I listen for that?
Thank you.
Upvotes: 2
Views: 6236
Reputation: 21718
Kurtis link describes numerous ways of checking the current network state.
To listen for the network change events, your activity must register the broadcast receiver that listens for the CONNECTIVITY_CHANGE:
public void onCreate(Bundle savedInstanceState) {
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Hey, net?");
}}, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
}
You also need to declare your intent filter in the manifest:
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
and also add android.permission.ACCESS_NETWORK_STATE permission.
Upvotes: 10
Reputation: 30825
Can you please tell me how can I detect network connectivity in android?
This has been asked and answered. See this stackoverflow post for details.
An example is when the airplane mode is on and off. How can I listen for that?
The ACTION_AIRPLANE_MODE_CHANGED is broadcast when ever the airplane mode changes.
Upvotes: 1