android - Long amount of time elapses before onServiceConnected() is called
问题描述
I have an activity and a service running in different threads. It is a sound recorder for a night time.
The problem comes when the service crashes for any reason (we can't avoid that crash, as it comes from a external library) in the middle of the recording.
What I do, as a workaround, is to restart the activity when the Activity notices that the connection with the service has been lost. By recreating the activity, the service is recreated too.
The problem is that, it takes a lot of time (even 1 hour) between the call to
bindService(RecorderService.newIntent(this), this, BIND_AUTO_CREATE);
and the callback
public void onServiceConnected(ComponentName name, IBinder service) {
I think this happens because the phone is in a kind of low-battery mode, as it is not used by the user in the middle of the night.
I call startService once the connection is done, as it is needed that the Activity and the Service is connected.
So the problem is that the record stops for that time, and I would need it to be running without interruptions. I could call startService once the activity is restating, but that would not ensure that the service would be connected at the time of another hypotethical crash.
Any idea about how to make the connction/start of the service faster?
Thanks in advance!
解决方案
If the problem is only the "low power mode" during the night I think you can aquire a WakeLock (https://developer.android.com/reference/android/os/PowerManager.WakeLock) to wake-up the CPU just before starting the Service or Activity. Don't forget to release the Lock when you've done ;)
推荐阅读
- vba - VBA Word,获取每个不同数组值的不同计数
- django - 过滤到预取 django
- python - 确保 pandas 数据框的列具有唯一值
- html - 需要在离子头上的离子卡
- javascript - 如果对象具有重复属性,则从列表中删除对象
- python - selenium web driver 无法读取单页架构中的网络调用,主要是后续页面(selenium/chrome web driver/Python)
- python - 我尝试使用烧瓶部署我的机器学习模型并得到错误 SystemExit: 1
- vba - 转发 Outlook 会议
- javascript - 仅在小屏幕上使用按钮更改 div 的高度
- batch-file - 无法使用带有特殊字符的批处理