我正在尝试创建一个Android应用程序,在设备屏幕关闭时实时连续记录设备位置数据.我的代码可以在Android 6.0及更早版本中正常运行,但似乎Android 7.0+破坏了我的应用.
我已经实现了一个使用唤醒锁的Android前台服务,并订阅了Google FusedLocation API.关闭屏幕后,onLocationChanged永远不会触发回调.
有谁见过这个问题的解决方案?我尝试通过Android应用的Android设备设置以及Google服务框架和融合位置禁用电池优化.
public class ForegroundLocationService extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private static final String TAG = ForegroundLocationService.class.getSimpleName();
// the notification id for the foreground notification
public static final int GPS_NOTIFICATION = 1;
// the interval in seconds that gps updates are requested
private static final int UPDATE_INTERVAL_IN_SECONDS = 15;
// is this service currently running in the foreground?
private boolean isForeground = false;
// the google api client
private GoogleApiClient googleApiClient; …Run Code Online (Sandbox Code Playgroud) 我的应用必须持续跟踪用户.为此,我有一个LocationListener应该连续接收位置更新.问题是当屏幕关闭时,它不会收到任何更新.
我试过添加一个部分唤醒锁:
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);
mLocationClient = new LocationClient(ADS.getAppContext(), new LocationGatherer(), new LocationGatherer());
mLocationClient.connect();
PowerManager pm = (PowerManager) ADS.getAppContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
Run Code Online (Sandbox Code Playgroud)
但它仍然没有收到更新.
我正在使用asmack库实现聊天应用程序.我想启动一个始终运行的服务,其中所有的连接管理任务都要完成,并且应该继续接收传入的数据包并将消息保存在我的sqlite数据库中.当我的应用程序启动时,我希望我的应用程序也会收到通知消息,而后台服务将消息插入sqlite数据库.如何用粘性服务实现这样的结构.是否还需要部分唤醒锁定.因为,有时也会根据特定需求使用http来下载图像.
哪种方法更好,直接实现LocationListener这样
public class BackgroundService extends Service implements LocationListener {}
Run Code Online (Sandbox Code Playgroud)
或者通常LocationListener在课堂内宣布?
LocationListener locationListener = new LocationListener() {};
Run Code Online (Sandbox Code Playgroud)