Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver when wifi or 3g network state changed

I have an app which updates the database whenever the phone is connected to WiFi. I have implemented a Service and BroadcastReceiver which will run the Service (it will tell me what network is in use), but the problem is I don't know what to add in the manifest file to start BroadcastReceiver when the network state changes or when it connects to some kind of network

like image 449
J1and1 Avatar asked May 24 '12 07:05

J1and1


People also ask

When would you use a BroadCastReceiver?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

Which method will be called when intent broadcast shall be received by BroadCastReceiver?

The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context.

What is a BroadCastReceiver?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.


1 Answers

You need

<intent-filter> <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/> <action android:name="android.net.wifi.STATE_CHANGE"/> </intent-filter> 

In your receiver tag.

Or if you want more control over it, before registering BroadcastReceiver set these up:

final IntentFilter filters = new IntentFilter(); filters.addAction("android.net.wifi.WIFI_STATE_CHANGED"); filters.addAction("android.net.wifi.STATE_CHANGE"); super.registerReceiver(yourReceiver, filters); 

WIFI_STATE_CHANGED

Broadcast <intent-action> indicating that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown. One extra provides this state as an int. Another extra provides the previous state, if available.

STATE_CHANGE

Broadcast <intent-action> indicating that the state of Wi-Fi connectivity has changed. One extra provides the new state in the form of a NetworkInfo object. If the new state is CONNECTED, additional extras may provide the BSSID and WifiInfo of the access point. as a String

Also, you'll need to specify the right permissions inside manifest tag:

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

To check connectivity, you can use ConnectivityManager as it tells you what type of connection is available.

ConnectivityManager conMngr = (ConnectivityManager)this.getSystemService(this.CONNECTIVITY_SERVICE); android.net.NetworkInfo wifi = conMngr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); android.net.NetworkInfo mobile = conMngr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
like image 168
nullpotent Avatar answered Sep 17 '22 17:09

nullpotent