I is there a way to Flag if a WIFI connection got disconnected/ dropped off OR if the user actually changed the WIFI network ?
I need my app to do : Connect to a WIFI XYZ, if XYZ get disconnect (FLAG 1) or dropped off Then reconnect to XYZ. But is the user change to another wifi BTOpen (FLAG 2) then allow the connect and Stop my service. If user connect to XYZ again then start the loop again.
What I got so far is :
<!-- WIFI Receiver -->
<receiver android:name=".ReceiverWifi" >
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service android:name=".ServiceWifiMonitor" />
<receiver android:name=".ServiceController" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
BroadcastReceiver:
myApplication = (MyApplication) context.getApplicationContext();
conManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
networkInfo = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isConnected = networkInfo != null && networkInfo.isConnected();
int reconnectedCount = myApplication.getReconnectedCount();
if (wifiManager.isWifiEnabled()) {
if("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
//Start and Stop Service
if(myApplication.isReconnect()) startServiceWifiMonitor(); else stopServiceWifiMonitor();
if (isConnected) {
//There is a WIFI Connection
myApplication.setConnectedWifi(NetworkUtil.getCurrentSSID(context));
myApplication.setWifiStatus("connected");
if (NetworkUtil.isConnectedToXYZ(context)) {
startServiceWifiMonitor();
if(pref.getisFirstTime())
{
myApplication.setWifiByChoise("XYZ");
pref.setisFirstTime(false);
}
else { myApplication.setisReconnect(true); }
}
else {
//Connected to different NetWork
if(myApplication.isReconnect() && NetworkUtil.isXYZAvailable(context))
{
//ReConnect to XYZ
NetworkUtil.connectToXYZ(context);
myApplication.setReconnectedCount(reconnectedCount++);
}
else { resetValues("AAAA"); }
}
}//end if
else
{
if(NetworkUtil.isXYZAvailable(context) && myApplication.getWifiByChoise().equals("XYZ"))
{
NetworkUtil.connectToXYZ(context);
myApplication.setReconnectedCount(reconnectedCount++);
}
else { resetValues(""); }
}
}//end CONNECTIVITY_CHANGE
Service Monitor:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand > Received start id " + startId + ": " + intent);
objHandler.postDelayed(mTasks, 1000);
return START_STICKY;
}//end onStartCommand
private Runnable mTasks = new Runnable() {
public void run() {
if(myApplication.getWifiByChoise().equals("XYZ") && NetworkUtil.isXYZAvailable(context)) {
try
{
//Get the numbers of Reconnection
int count = myApplication.getReconnectedCount();
if(!NetworkUtil.isWifiConnected(context))
{
NetworkUtil.connectToXYZ(context);
myApplication.setisReconnect(true);
myApplication.setReconnectedCount(count++);
}
if(!NetworkUtil.isConnectedToXYZ(context))
{
NetworkUtil.connectToXYZ(context);
myApplication.setisReconnect(true);
myApplication.setReconnectedCount(count++);
}
} catch (Exception e) {e.printStackTrace();}
}
else { stopSelf(); }
int ms_interval = 3000;
objHandler.postDelayed(mTasks, ms_interval);
}
};//end Runnable mTasks
The problem with my app is that : It crashed the device, Seems like its eating up all the memory ram. sometimes with the wifi XYZ get disconnect it wont connect again and if user change to another wifi, it won't allow the connection.
I really appreciate your help. Thank you.
Top 10 fixes for when WiFi Keeps disconnecting on Android:Restart your Wi-Fi router. Move closer to the WiFi network source. Try switching the router's AP band. Disable network auto-switch.
Open the system menu. Click the WiFi icon and go to WiFi settings. Press the menu button in the top-right corner of the window and select Connect to Hidden Network.
Tap Settings > Wireless & Networks > Wi-Fi as shown in Figure 1. If Wi-Fi is off, tap the slider to turn Wi-Fi on. When Wi-Fi is on, a signal indicator appears at the top right corner of your home screen.
Check the network connected Name by using:
public String getWifiName(Context context) {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (manager.isWifiEnabled()) {
WifiInfo wifiInfo = manager.getConnectionInfo();
if (wifiInfo != null) {
DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) {
return wifiInfo.getSSID();
}
}
}
return null;
}
if this name matches your networkSSID, i.e. XYZ, then resume the service, else if it doesn't match, then stop the service:
if getWifiName(this).compareTo("XYZ") == 0 { //XYZ is your network name on which you want to resume the service
//code to resume
} else {
//code to stop the service
}
This is how I handle it in my app:
public class WifiStateWatcher extends BroadcastReceiver {
private MainActivity activity;
public WifiStateWatcher(MainActivity activity) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
}
@Override
public void onReceive(Context context, Intent intent) {
SupplicantState supState;
WifiManager wifiManager = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();
if (supState.equals(SupplicantState.COMPLETED)) {
//we are connected to Wi-Fi network
} else {
//we lost Wi-Fi connectivity
}
}
}
You will need android.permission.ACCESS_WIFI_STATE
permission
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