I need to receive broadcasts for network actions like network connected, disconnected etc. I am using a broadcast receiver for this purpose. Can anyone please tell me which intent action I need to capture for network events, right now as per my search on internet I am using android.net.ConnectivityManager.CONNECTIVITY_ACTION.
Here is my broadcast receiver class:
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class NetworkStateReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction().equals( android.net.ConnectivityManager.CONNECTIVITY_ACTION)) { // do something.. } } }
and I have also added permission for accessing network state:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
here is how I have declared this class in manifest file
<receiver class=".NetworkStateReceiver" android:name=".NetworkStateReceiver"> <intent-filter> <action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" /> </intent-filter> </receiver>
Please suggest me the right intent action if I am wrong OR if there is any other way to catch network events.
Intent is to perform an action. It is mostly used to start activity, send broadcast receiver, start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Intent send = new Intent(MainActivity.
In addition to providing a mechanism for launching application activities, intents are also used as a way to broadcast system wide messages to other components on the system. This involves the implementation of Broadcast Intents and Broadcast Receivers, both of which are the topic of this chapter.
An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object.
Here's a working example:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <receiver android:name=".receiver.ConnectivityReceiver"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
.
public class ConnectivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d(ConnectivityReceiver.class.getSimpleName(), "action: " + intent.getAction()); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With