【发布时间】: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