我正在编写一个使用gps跟踪和记录设备位置的应用程序,它在API 21上运行良好,但是当我切换到API 26时,它停止了工作。
我使用了请求位置更新来定期发送待处理的意图:
public void startLocationUpdates() throws SecurityException
{
String provider = LocationManager.GPS_PROVIDER;
PendingIntent pi = getLocationPendingIntent(true);
//this method keep updating unless we specify it to stop
mLocationManager.requestLocationUpdates(provider, 0, 0, pi);
}
我正在与此接收方处理待处理的意图:
public class LocationReceiver extends BroadcastReceiver
{
private static final String TAG = "LocationReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "Receive an intent");
Location= (Location)intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);
Log.d(TAG, this + " Got location from " + loc.getProvider() + ": " + loc.getLatitude() + ", " + loc.getLongitude());
}
}
我认为这与以下行有关:
2019-02-08 21:41:05.872 1995-2015/system_process W/BroadcastQueue: Background execution not allowed: receiving Intent { act=com.pproject.runtracker.ACTION_LOCATION flg=0x10 (has extras) } to com.pproject.runtracker/.Controller.LocationReceiver
我不确定这是什么意思
答案 0 :(得分:1)
这意味着您的应用必须先“ foreground”才能频繁请求/提供位置数据。 因此,您需要实施前台服务并通知有关GPS的使用。 您不应该使用implicit broadcasts。对于一对一链接,我使用回调模式,并且可以使用,对于一对多链接,您可以使用Messenger或EventBus。
我也建议您使用融合位置提供程序,因为它更有效,并且可以消除“冷启动”之类的问题。