英文:
Android slows down foreground service response during sleep
问题
我正在测试我的应用前台服务,在它从“server/website”收到响应时应通知我(使用Handler + 每7秒运行一次的Runnable来检查服务器上的更改),我注意到一些异常情况,当手机处于睡眠模式时:
首先,在第一分钟,一切似乎都正常工作,但之后,服务似乎有延迟。它在服务器更改后的一分钟后才给我响应,然后是2分钟,然后是5分钟,甚至8分钟。然后又是1分钟...如此循环。
只有当手机未连接到笔记本电脑时才会发生这种情况。我曾尝试使用Android Studio进行调试,但当它通过数据电缆连接到笔记本电脑时,一切都正常。
就好像它停止了应用程序访问互联网。我之所以这么说是因为当我收到WhatsApp的通知时,我也会收到我的应用程序通知。
Android是否会减慢/限制应用程序的互联网访问速度?还是有其他问题我没有注意到?
我使用两部手机:Asus Zenfone和Samsung Galaxy a5。三星似乎工作得稍微好一些,但是...
英文:
I'm testing my app foreground service, who should notify me, when it gets a response from "server/website" (Handler + Runnable every 7 sec checks for changes on server), and I noticed something unusual, when the phone is in sleep mode:
At first, in the first minute, all seems to work fine, but afterwords, the service it seems delayed. It gives me a response after a minute from the server change,then 2 then 5, even 8 minutes. Then again 1 minute ... an so on
This is only happening when the phone is not connected with the laptop. I've tried to debug it with android studio but, when it was connected (with the data cable) to the laptop, all worked fine.
It's like it stops giving the app access to internet. And I'm saying this because when I get a notification from WhatsApp, I'll get my app notification as well.
Can android slow/throttle the internet access to the app? Or is there another problem that I'm not seeing?
I use two phones: Asus Zenpone and a Samsung Galaxy a5. The Samsung seems to work a little better, but ...
答案1
得分: 0
我成功地通过参考安卓开发者文档中的解决方案解决了这个问题:
https://developer.android.com/training/scheduling/wakelock
在服务类中:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyApp::MyWakelockTag");
wakeLock.acquire();
在清单文件中:
<uses-permission android:name="android.permission.WAKE_LOCK" />
英文:
I managed to solve this problem with a solution from android developers docs:
https://developer.android.com/training/scheduling/wakelock
In the service class:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyApp::MyWakelockTag");
wakeLock.acquire();
In the manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论