防止Oreo或Pie在后台时暂停LocationListener。

问题描述 投票:0回答:1

我做了一个我个人使用的应用程序(徒步旅行时的GPS跟踪,并将位置发送到另一部手机上,用于调查)。

它是在Lolipop下,工作正常。

新手机,在Pie下。

如果应用程序在前台,工作正常,但在后台时被Pie设置为暂停。

如何保持它在后台工作?

我试过在"设备护理" > "蓄电池" > "应用程序电源管理"取消检查"自适应电池"或"让未安装的应用程序进入睡眠状态"不工作.我把我的应用程序在 "应用程序不会被放到睡眠",但不工作了。

有没有一种 "简单的方法 "可以让我的应用在后台保持活跃?

AndroidManifest?或者和屏幕一样。getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

我的应用是基于 onLocationChanged(final Location location)

谢谢你的帮助敬上

android background locationlistener
1个回答
0
投票

其中一个选项是使用 报警管理器 来请求接收器的运行时间(例如,每分钟,甚至每几秒钟),或请求从 位置管理器


0
投票

经过大量的搜索,我想我在这里找到了解决方案。https:/developer.android.comtraininglocationpermissions

问题是我得到了API29(安卓10),需要新的权限(在安卓8或9不需要)。

它在androidManifest.xml中。

如果在前景。

<!-- Recommended for Android 9 (API level 28) and lower. -->
<!-- Required for Android 10 (API level 29). -->
<service
    android:name="MyNavigationService"
    android:foregroundServiceType="location" ... >
    <!-- Any inner elements would go here. -->
</service>

或者如果在背景中,

<manifest ... >
  <!-- Required only when requesting background location access on
       Android 10 (API level 29). -->
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
</manifest>

我只测试前景方案 android:foregroundServiceType="location" 并且工作正常。

编程是很难的工作。知识更难。

玩得开心点。谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.