1

I have created an android app which runs fine in all phones. But in my Alcatel phone it doesn't as the phone goes to a deep sleep mode and the data network fails so the app doesn't get a data network and doesn't sync the data from the server .


My Design ...

SystemBootReceiver --> (DataSyncService)Service --> (MyBroadcastReceiver)BroadcastReceiver --> (MyDataService)Service .

So here on system boot I start DataSyncService where I set up the AlarmManager (repeated) and call the MyBroadcastRecever. After calling the BroadcastRecever I stop DataSyncService by calling stopself() .

Now the MyBroadcastRecever calls the MyDataService.


I came across WakeLocks which as said prevent the phone from going in deep sleep mode. So I implemented it inside MyDataService onCreate() method

PowerManager pm = (PowerManager) 
                    getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
mWakeLock.acquire();

And release() the wake lock before stopping the service.

I have also set the permission in android Manifest.

But this didn't work. So for a quick check I used WAKE LOCK app from the market .

But this also didn't wake the phone up. Again I came across WAKE MY ANDROID (app removed from store) app from market and installed it .. and a magic happened here.

It kept the phone alive.

As the description in this app says that they have also used a Wake Lock. So what am I missing then ?

Is there an implementation mistake or a design issue ?

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
Tushar Agarwal
  • 521
  • 1
  • 16
  • 39
  • Perhaps, you could try [ACQUIRE_CAUSES_WAKEUP](http://developer.android.com/reference/android/os/PowerManager.html#ACQUIRE_CAUSES_WAKEUP) flag with SCREEN_DIM_WAKE_LOCK – Alexander Kulyakhtin Sep 14 '12 at 03:34
  • Can you clarify your problem? The problem is that network requests fail when the app runs after waking up the device? – satur9nine Apr 29 '13 at 17:28
  • Did you solve your problem ? – Mr_and_Mrs_D Nov 14 '13 at 21:05
  • @Mr_and_Mrs_D yes i have solved the problem with the answer you gave . thanks , also i came across WakefulBroadcastReceiver https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html – Tushar Agarwal Dec 14 '13 at 10:41

1 Answers1

4

The lock you have in your service never runs - cause your service is never run

Well - you have to use a WakefulIntentService - make your MyDataService extend WakefulIntentService and in your receiver call WakefulIntentService.doWakefulWork(context, MyDataService.class)

Or implement a static lock shared by both your service and receiver - which would be tricky.

If using wifi you should also need to use a WifiLock - inside your wakeful intent service - can get tricky

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • I think the asker indicated their service does run but that there is a network error. – satur9nine Apr 29 '13 at 17:47
  • @satur9nine:"But in my Alcatel phone it doesn't as the phone goes to a deep sleep mode..." - please revert – Mr_and_Mrs_D Apr 29 '13 at 17:56
  • Mr_and_Mrs_D:"But in my Alcatel phone it doesn't as the phone goes to a deep sleep mode and the data network falls so the app doesn't get a data network and doesn't sync the data from the server ." I see the problem as being "app doesn't get a data network" – satur9nine Apr 29 '13 at 18:09
  • @satur9nine:it's not - and even then he will need wifi locks - BUT AFTER the CPU is awake, obviously. Fair play please – Mr_and_Mrs_D Apr 29 '13 at 18:16
  • My vote is locked in by StackOverflow I cannot change it. Using a WifiManager.WifiLock is a more useful suggestion, but only works if the user is in range of Wi-Fi. – satur9nine Apr 29 '13 at 18:24
  • @satur9nine: the problem is he does not keep the phone awake - mobile data would but I bet he is using wireless - and now you can change your vote :) – Mr_and_Mrs_D Apr 29 '13 at 18:28
  • I don't think he wants to keep the phone awake forever, he just wants it to wake up periodically, use some network and go back to sleep. – satur9nine Apr 29 '13 at 18:30
  • @satur9nine:Yes exactly that is what the Wakeful_IntentService_ is for - the lock is disposed as soon as the service returns - a simple intent service won't run probably - check the links I posted - thanks for upvote :) – Mr_and_Mrs_D Apr 29 '13 at 18:36