Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent action for network events in android sdk

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.

like image 469
mudit Avatar asked Feb 19 '10 08:02

mudit


People also ask

What is intent explain the types of intents in Android with example?

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.

What is activity intent and Broadcasting intent?

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.

What is Android intent Action view?

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.


1 Answers

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());     }  } 
like image 79
yanchenko Avatar answered Sep 28 '22 10:09

yanchenko