由于无法从TLocationSensor获取位置结果(RAD Studio示例应用程序在Rio无法使用),因此我检查了Internet并找到以下示例:
procedure TForm1.Button1Click(Sender: TObject);
var
LocationManagerService: JObject;
iter : JIterator;
location : JLocation;
begin
if not Assigned(FLocationManager) then
begin
LocationManagerService := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
FLocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID);
if not Assigned(locationListener) then
locationListener := TLocationListener.Create(self);
FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 1000, 0, locationListener,
TJLooper.JavaClass.getMainLooper);
end;
iter := FLocationManager.GetAllProviders.Iterator;
ListBox1.Clear;
while iter.hasNext do
begin
ListBox1.Items.Add(JStringToString(iter.next.ToString));
end;
CheckBox1.IsChecked := FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
CheckBox2.IsChecked := FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER);
location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
if assigned(location) then onLocationChanged(location);
end;
此代码在一个Android 8平板电脑上可以完美地运行,但是某些带有Lineage OS Android 7的三星Galaxy S3出现了一些问题。
在此设备上,该代码只能运行一次,现在任何时间位置为零。
什么可能是原因,getLastKnownLocation结果为nil并且只能工作一次?是否有可能重置它?