I am develping an app in which whenever wifi turned on then in background app should be to connect to a network if the network it connected to previously ?Can i use broad cast reciver for this purpose?
Asked
Active
Viewed 5,033 times
1
-
I think you need to used `BroadcastReceiver` as well as other components such as `WifiManager` and `NetworkManager` – hoomi Sep 11 '15 at 06:46
-
can i detect if wifi network availble using brodcast reciver & then connect to the network which previosly remember. – TechChain Sep 11 '15 at 06:47
-
Why not. I guess you have to first read the saved Wi-Fi networks and then listen to the broadcasts. – hoomi Sep 11 '15 at 06:49
-
This will get you on the right track. http://stackoverflow.com/questions/27559837/how-to-trigger-broadcastreceiver-when-i-turn-on-off-mobile-cellular-datamobile – Jibran Khan Sep 11 '15 at 06:58
-
I want to listen to broadcast first & then i get the previous saved wifi ssid & password from database then i try to match with searched result.if reslut match with previous then i connect to that network – TechChain Sep 11 '15 at 06:58
-
hi , please check this link http://stackoverflow.com/questions/8818290/how-to-connect-to-a-specific-wifi-network-in-android-programmatically – Sep 11 '15 at 06:59
1 Answers
1
For receiving the Broadcast
for connectivity change, you can listen like this:
Register your receiver in the AndroidManifest.xml
<receiver
android:name=".NetworkCheckReceiver"
>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Then in the BroadcastReceiver
class
public class NetworkCheckReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Log.d("NetworkCheckReceiver", "NetworkCheckReceiver invoked...");
boolean noConnectivity = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (!noConnectivity) {
Log.d("NetworkCheckReceiver", "connected");
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", networkSSID);
wifiConfig.preSharedKey = String.format("\"%s\"", networkKey);
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
}
else{
Log.d("NetworkCheckReceiver", "disconnected");
}
}
}
}
And of course, don't forget to add the required permissions
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Jibran Khan
- 3,236
- 4
- 37
- 50
-
what is networkSSID and networkKey here. If we dont have any then what to do in that case? – Amarjit Apr 12 '17 at 04:47
-
@theLazyFinder the purpose of above code is auto connect to a wifi connection therefore SSID and key will have to be defined. SSID is the WiFi connection name and key is its password to connect – Jibran Khan Apr 12 '17 at 13:45