4

I am building application which can get location using NETWORK_PROVIDER periodically in background without using GPS_PROVIDER. I am using ALARM_SERVICE and WakeLock(PARTIAL_WAKE_LOCK). but the problem which i am facing is that internet connection gets disconnected once the screen goes off. when I unlocks the phone I starts receiving the location, but when the screen goes off I am not getting the locations.

Is it because:

  1. Internet connection gets paused once the screen goes off and also when I unlocks the screen I get the USSD code messages of Data Usage, so does it means my internet connection goes off once the screen goes off?
  2. Even though the internet connection is on but location doesn't gets updated in background as the screen is in off state.

I am using GpsTracker class to get location from here and using AlarmManager get location periodically. also in LatLongBroadcastReceiver class i am fetching a location.

Intent intent = new Intent(GPSlatlongActivity.this,
                    LatLongBroadcastReceiver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

pendingIntent = PendingIntent.getBroadcast(
        GPSlatlongActivity.this.getApplicationContext(), 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime(),
        (AlarmManager.INTERVAL_FIFTEEN_MINUTES / 15),
        pendingIntent);
jww
  • 97,681
  • 90
  • 411
  • 885
Amol Sawant
  • 13,842
  • 2
  • 20
  • 27
  • Are you testing it on real device? Few applications used for saving battery like juiceDefender turns mobile internet off when the screen is locked. Kindly check if any such app installed in device – Prem Nov 16 '13 at 06:04
  • yes I am testing on real device. tried on 4-5 device. No such app installed which will pause internet connection when screen goes off. – Amol Sawant Nov 16 '13 at 10:04
  • If I understand correctly, your question is: "Is it possible that my device disconnects from the internet when the screen locks?" Assuming your location logic is working, is that formulation accurate? – Paul Lammertsma Nov 21 '13 at 19:16
  • Did you try and put some extra log output in your code, and hook up the device for usb debugging? Should have a pretty straight shot at finding out if the connection is active, or if something else goes wrong. – bgse Nov 21 '13 at 22:16
  • @Paul, yes right. is there any solution for this? i am struggling with problem by half month. Please let me know if any solution for this. – Amol Sawant Nov 23 '13 at 05:17

2 Answers2

6

According to documentation WifiManager.WifiLock

Allows an application to keep the Wi-Fi radio awake. Normally the Wi-Fi radio may turn off when the user has not used the device in a while. Acquiring a WifiLock will keep the radio on until the lock is released. Multiple applications may hold WifiLocks, and the radio will only be allowed to turn off when no WifiLocks are held in any application.

I guess you need this lock. Acquire this lock if the device is connected to WiFi. But please

Note that WifiLocks cannot override the user-level "Wi-Fi Enabled" setting, nor Airplane Mode. They simply keep the radio from turning off when Wi-Fi is already on but the device is idle.

M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • hello M-WaJeEh, Thank you for answer. is this keep android CPU running? – Amol Sawant Nov 23 '13 at 09:25
  • If i used PowerManager.FULL_WAKE_LOCK then is giving me location update but if I used PowerManager.PARTIAL_WAKE_LOCK. using FULL_WAKE_LOCK screen goes turn on and giving me updated location but i dont want to make screen on. thats why i used PARTIAL_WAKE_LOCK, after that i am getting updated location when screen goes off. Please help me. thank you in asvance – Amol Sawant Nov 23 '13 at 09:30
  • 4
    Use `PowerManager.PARTIAL_WAKE_LOCK` with `WifiManager.WifiLock`. – M-Wajeeh Nov 23 '13 at 14:03
5

Unless your application has a very good reason to keep a Wi-Fi connection, you should really try to respect the user's preference to disable WiFi when the screen locks. Your application will quickly cause the device to consume significantly more power by using any sort of lock. As mentioned by others, in your case, you'll need the PowerManager.PARTIAL_WAKE_LOCK to keep the CPU running (without preventing the screen from locking) and WifiManager.WakeLock to keep Wi-Fi connected.

You'll probably find the setting causing this behaviour under the system settings for Wi-Fi, under advanced Wi-Fi settings:

Keep Wi-Fi on during sleep

By disabling this setting on a device with mobile data, your network location listener should still work as Android will continue to use Google's Location API in the background to resolve the device's location based on cellular towers.

If your device is also switching off mobile data, perhaps some power-saving application is disabling it. BlueEFFICIENCY and Juice Defender are some apps that have an option for this optimization, but HTC and Sony have included some power saving options on their devices that may also be exhibiting this behavior.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187