Android deep sleep and wake locks

≡放荡痞女 提交于 2019-12-30 12:03:14

问题


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 ?


回答1:


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



来源:https://stackoverflow.com/questions/12417541/android-deep-sleep-and-wake-locks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!