【问题标题】:LocationManager.requestLocationUpdates with PendingIntent did not work on API 26带有 PendingIntent 的 LocationManager.requestLocationUpdates 在 API 26 上不起作用
【发布时间】:2019-02-08 15:03:48
【问题描述】:

我正在编写一个使用 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

我不确定这是什么意思

【问题讨论】:

  • 如何注册广播接收器?如果您只是在清单中声明而不以编程方式注册,这就是问题所在,因为 api 24 您必须在代码中注册您的广播。
  • @MaksimNovikov 好的,代码在 java 代码中使用接收器时有效。问题是广播是明确的。感谢您的帮助
  • 欢迎@PedroOscar

标签: java android


【解决方案1】:

这意味着您的应用不能频繁地请求/提供位置数据,除非是“foreground”。 所以你需要实现前台服务并通知GPS的使用。 你不应该使用implicit broadcasts。对于一对一链接,我使用回调模式并且它可以工作,对于一对多,您可以使用 Messenger 或 EventBus。

我还推荐使用 Fused Location Provider,因为它更省电并且可以消除“冷启动”等问题。

【讨论】:

  • 但是我的应用程序不是已经在前台,因为“如果一个应用程序有一个可见的活动,则它被视为前台,无论活动是启动还是暂停。”
  • 我更新了答案,隐式广播问题github.com/AfzalivE/AwakeDebug/issues/3
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
  • 2013-04-06
  • 1970-01-01
相关资源
最近更新 更多