我想在手机启动时接收设备的位置更新(在Pixel 3a上进行测试)。这是常规应用程序的一项附带功能,因此请确保已获得必要的权限授予等。
以下代码也摘自从MainActivity调用时可以正常工作的组件。
设置位置回调的步骤如下:
首先,侦听ACTION_LOCKED_BOOT_COMPLETED的DirectBootAware BroadcastReceiver意味着我可以在设备启动时以及屏幕解锁之前运行代码。
[BroadcastReceiver(Enabled = true, Exported = true, DirectBootAware = true)]
[IntentFilter(new string[] { Intent.ActionLockedBootCompleted })]
public class StartForegroundServiceReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
从这里我可以验证是否授予了Corse和Fine位置的权限(它们是!)
var permissionCheckResult = context.CheckSelfPermission(Manifest.Permission.AccessFineLocation);
Android.Util.Log.Info("TestingTag", $"AccessFineLocation CheckSelfPermission {permissionCheckResult.ToString()}");
permissionCheckResult = context.CheckSelfPermission(Manifest.Permission.LocationHardware);
Android.Util.Log.Info("TestingTag", $"LocationHardware CheckSelfPermission {permissionCheckResult.ToString()}");
permissionCheckResult = context.CheckSelfPermission(Manifest.Permission.AccessCoarseLocation);
Android.Util.Log.Info("TestingTag", $"AccessCoarseLocation CheckSelfPermission {permissionCheckResult.ToString()}");
并且启用了网络,GPS和被动位置提供程序(同样,它们都已启用!)
var locationManager = (LocationManager)context.GetSystemService(Context.LocationService);
var enabled = locationManager.IsProviderEnabled(LocationManager.PassiveProvider);
Log.Info("TestingTag", $"PassiveProvider IsProviderEnabled {(enabled ? "yes" : "no")}");
enabled = locationManager.IsProviderEnabled(LocationManager.GpsProvider);
Log.Info("TestingTag", $"GpsProvider IsProviderEnabled {(enabled ? "yes" : "no")}");
enabled = locationManager.IsProviderEnabled(LocationManager.NetworkProvider);
Log.Info("TestingTag", $"NetworkProvider IsProviderEnabled {(enabled ? "yes" : "no")}");
如果我选中GetLastKnownLocation
var location = locationManager.GetLastKnownLocation(LocationManager.GpsProvider);
if (location == null)
{
location = locationManager.GetLastKnownLocation(LocationManager.NetworkProvider);
}
if (location == null)
{
location = locationManager.GetLastKnownLocation(LocationManager.PassiveProvider);
}
if (location == null)
Log.Error("TestingTag", $"Location is null");
这总是返回null,也许是因为设备刚刚启动了?还是可能是由于与未收到任何位置更新相同的根本原因?
最后,我使用LocationManager
var callback = new LocationCallback();
Log.Info("TestingTag", "Starting RequestLocationUpdates via PassiveProvider..");
locationManager.RequestLocationUpdates(LocationManager.PassiveProvider, 3000, 3, callback);
Log.Info("TestingTag", "Starting RequestLocationUpdates via GpsProvider..");
locationManager.RequestLocationUpdates(LocationManager.GpsProvider, 3000, 3, callback);
Log.Info("TestingTag", "Starting RequestLocationUpdates via NetworkProvider..");
locationManager.RequestLocationUpdates(LocationManager.NetworkProvider, 3000, 3, callback);
这是位置回调代码:
public class LocationCallback : ScanCallback, Android.Locations.ILocationListener
{
public void OnLocationChanged(Location location)
{
Log.Info("TestingTag", $"OnLocationChanged {location.Latitude} ~ {location.Longitude}");
}
public void OnProviderDisabled(string provider)
{
Log.Info("TestingTag", $"OnProviderDisabled {provider}");
}
public void OnProviderEnabled(string provider)
{
Log.Info("TestingTag", $"OnProviderEnabled {provider}");
}
public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
{
Log.Info("TestingTag", $"OnStatusChanged {provider} ~ {status.ToString()}");
}
}
永远不会调用回调:-(
我添加了一个BluetoothManager
来扫描BLE信号(该代码省略了该代码),并且可以正常工作,ScanResults返回的很好,并且电话/ BroadcastReceiver仍然有效。因此,这似乎不是BroadcastReceiver快要死了...
任何提示/建议/或确认我尝试做的事情都是不可能的,将不胜感激!
谢谢!