gpt4 book ai didi

android - 如何在应用程序处于后台时保持 GPS 接收器位置更新(Google Play Service Location API)

转载 作者:行者123 更新时间:2023-11-29 18:45:10 25 4
gpt4 key购买 nike

抱歉,这是一个很长的问题,但我希望你们中的一位专家可以帮助正在悄悄发疯的新手!!

我有一个 Android 应用程序,它使用后台服务每 20 秒获取一次 GPS 定位(使用 Google Play 服务)。它将纬度和经度与列表中的经度和纬度进行比较,如果找到匹配项,它会向接收器发送广播,触发前台 Activity 以提醒用户。

我使用后台服务是因为用户提醒之间通常有 2 到 20 分钟的间隔,并且在此期间没有用户交互。该应用使用一个前台 Activity 供用户选择他想要的选项,然后关闭所有前台 Activity,只留下后台 Activity 运行。

这在我的旧设备上的 Android 4.3 上运行良好,但我现在更新它在 Android 8 (Oreo) 上运行(在 Sony Xperia XZ1Compact 上测试)。我已经添加了

<uses-permission 
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/

到 list ,这导致应用程序在首次运行时请求许可。然后我的设备设置显示该应用程序允许省电异常。

后台服务的代码(抱歉,代码有点多,但我把它都包含进来以防相关!)如下

package com.barney.trackgps;

import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;



public class ApiTrackService extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {

GoogleApiClient mLocationClient;
LocationRequest mLocationRequest = new LocationRequest();
public static final String ACTION_LOCATION_BROADCAST =
ApiTrackService.class.getName() + "LocationBroadcast";
public static final String EXTRA_LATITUDE = "extra_latitude";
public static final String EXTRA_LONGITUDE = "extra_longitude";

int interval=20; //time between fixes in seconds


@Override
public void onDestroy(){
super.onDestroy();
stopSelf();
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("sTag","Got to apitrack");
mLocationClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest.setInterval(interval*1000);
mLocationRequest.setFastestInterval(1000);
int priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
mLocationRequest.setPriority(priority);
mLocationClient.connect();
return START_STICKY;
}


@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}


@Override
public void onConnected(Bundle dataBundle) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return;
}

LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient,
mLocationRequest, this);
}


@Override
public void onConnectionSuspended(int i) {
}


//to get the location change
@Override
public void onLocationChanged(Location location) {
boolean locFound=false;
Log.v("sTag","Got a fix");
/*does stuff to compare latitude and longitude with places in a list
and sets locFound=true if it finds a match*/
if (location != null) {
GPSLog=GPSLog+"Found";
if (locFound) {
Log.v("sTag", "Sending broadcast");
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setAction("com.AboutMyJourney.posBroadcast");
sendBroadcast(intent);
}
}
}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
//Log.d(TAG, "Failed to connect to Google API");
}

}

只要它的前台 Activity 之一打开并可见,该应用程序就可以正常工作。使用 logcat 进行跟踪显示后台服务正在获取 GPS 修复并且 onLocationChanged 下的代码(参见上面的代码)每 20 秒运行一次。

如果我允许我的应用程序关闭其所有前台 Activity 并仅运行上面的后台服务,只要另一个(完全不相关的)应用程序是要求 GPS 位置是公开和可见的。

但是,如果我在屏幕上看不到任何使用 GPS 的应用程序,则 GPS 跟踪将停止; onLocationChanged 方法不再运行,我也不再获得 GPS 修复。

如果我随后打开我的应用程序的一个前台 Activity ,GPS 跟踪将再次启动,表明后台服务没有被终止。

我假设这与为实现省电/打瞌睡模式所做的更改有关,我只是没有正确理解它。然而,我能找到的所有建议似乎都表明,只要后台服务作为设备白名单中的异常(exception)被允许,它就会继续工作,但这显然没有发生。

让后台服务工作是我的首选,因为这意味着编写新代码的工作量最少!但是,一些对类似问题的回答建议改用带有通知的前台服务。这是否满足我的要求,即大部分时间没有可见的用户界面(可能除了通知),仅提示用户每隔几分钟做一些事情,但让他或她同时自由地做其他事情?它在旧版本的 Android 中也能正常工作吗?

任何比我更了解这一点的人(可能比我更了解它!)可以帮忙吗?

最佳答案

applicable standby/doze documentation

An app that is whitelisted can use the network and hold partial wake locks during Doze and App Standby. However, other restrictions still apply to the whitelisted app, just as they do to other apps. For example, the whitelisted app’s jobs and syncs are deferred (on API level 23 and below), and its regular AlarmManager alarms do not fire.

您的用例似乎属于“其他限制仍然适用”。

您的应用程序在不使用时会耗尽电池电量,这正是 App Standby 和 Doze 旨在抵消的行为。

关于android - 如何在应用程序处于后台时保持 GPS 接收器位置更新(Google Play Service Location API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52168945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com