Reputation: 19826
I currently listen for changes in the Wi-Fi in my Android application and for the most part it works OK, however when the user has there device is idle/sleep mode, as in when the screen is blank. If they walk into a Wi-Fi area and automatically connect I don't get the intent until the user has turned on the screen. This is not good and leads to complaints about my application. Can anyone help with why I don't get the intent until the screen wakes up?
Here is my code:
BroadcastReciever:
public class WifiReciever extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
String action = arg1.getAction();
if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){
return;
}
if (!noConnectivity)
{
if (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)
{
//Handle connected case
Log.e("E", "GOT CONNECTION");
}
}
else
{
if (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)
{
//Handle disconnected case
Log.e("E", "LOST CONNECTION");
}
}
}
If a running Service I have that as a variable:
WifiReciever mConnectionReceiver;
And I start and stop the receiver in the service with the following calls:
private synchronized void startMonitoringConnection()
{
IntentFilter aFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mConnectionReceiver, aFilter);
}
private synchronized void stopMonitoringConnection()
{
unregisterReceiver(mConnectionReceiver);
}
I call a WakeLock to make sure that I am getting CPU time like this:
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CicCPULock");
wl.acquire();
However even with all that I never receive the Connectivity changed intent if the screen is blank, it works 100% of the time when the screen is on bit if I a user walks into a Wi-Fi are and connects when the screen is blank I fail to get the intent, is anyone aware of something I am missing?
Upvotes: 2
Views: 543
Reputation: 817
Actually there are many problems with WakeLocks, WIFI and WIFILocks in Android. First of all check your WIFI policy to see what will happen when screen goes dark. It must be "Always on". Then try to obtain a WIFI Lock to prevent the wifi from sleeping because keeping the CPU on does not cause the WIFI to go off. For a sample code refer to:
Also note that you must have the permission to use WakkeLocks:
<uses-permission android:name="android.permission.WAKE_LOCK" />
There are many other issues in this area check this question, and refer to Google issue tracker about WIFI problems, it helps you alot and saves you a lot of time:
Android strange cross device behaviour
Upvotes: 1