Firemonkey为Android启用GPS服务



我想知道你是否可以在Firemonkey项目中启用GPS服务?位置传感器没有启用它。只有在启用GPS或您有网络位置的情况下,它才能获得GPS坐标。

在其他一些安卓应用程序中,它会询问你是否要启用GPS,如果你同意,它会为该应用程序启用GPS。我也想这么做。

我已经知道如何检查Android是否启用了GPS服务,但不知道如何启用它本身。

下面的代码是如何检查GPS是否启用的:

uses
  Androidapi.JNI.Location,
  Androidapi.JNIBridge,
  FMX.Helpers.Android,
  Androidapi.JNI.GraphicsContentViewText;
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
var
  locationManager : JLocationManager;
begin
  locationManager := TJLocationManager.Wrap( ((SharedActivity.getSystemService(TJContext.JavaClass.LOCATION_SERVICE)) as ILocalObject).GetObjectID);
  if locationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER) then
   ;   //do something   
  if locationManager.isProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER) then 
   ;   //do something  else
end;

如果你用Java编程,我也试过这样做。喜欢此链接:https://stackoverflow.com/a/5305835/2728408

procedure TForm1.Button1Click(Sender: TObject);
{$IFDEF ANDROID}
var
  Intent: JIntent;
{$ENDIF}
begin
{$IFDEF ANDROID}
  Intent := TJIntent.Create;
  Intent.addCategory(TJIntent.JavaClass.CATEGORY_ALTERNATIVE);
  Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString('3')));
  Intent.setClassName(StringToJString('com.android.settings'), StringToJString('com.android.settings.widget.SettingsAppWidgetProvider'));
  try
    //For Delphi 10 Seattle
    TAndroidHelper.Activity.sendBroadcast(Intent);
    //For older versions of Delphi
    //SharedActivity.sendBroadcast(Intent);
  except
    on e: exception do
    begin
      ShowMessage('Error: ' + e.Message);
    end;
  end;
{$ENDIF}
end;

我没有任何错误,但我的GPS也没有打开。

更新:它似乎被禁用以启用Android 4.0及更高版本的GPS。

您不能启用GPS,但您可以要求用户这样做:

procedure TForm1.GPSSettings;
{$IFDEF ANDROID}
var
  Intent: JIntent;
{$ENDIF}
begin
{$IFDEF ANDROID}
  Intent := TJIntent.Create;
  Intent :=     TJIntent.JavaClass.init(TJSettings.JavaClass.ACTION_LOCATION_SOURCE_SETTINGS);
  TAndroidHelper.Activity.startActivity(Intent);
{$ENDIF}
end;

位置传感器位置传感器由TLocationSensor组件包裹。

TLocationSensor在设备检测到移动时触发OnLocationChanged事件。您可以使用"距离"one_answers"精度"属性调整TLocationSensor的灵敏度。

Distance属性指定设备必须移动的最小距离(以米为单位),以便位置传感器重新定位设备并返回新的位置信息。例如,如果将"距离"设置为"10",则当移动"10米"时,TLocationSensor会触发OnLocationChanged事件。Accuracy属性表示传感器相对于设备实际所在的地理点在地理上定位设备的精度级别(以米为单位)。提示:您应该指定适用于您的应用程序的最低精度;精度越高,传感器确定位置所需的时间和功率就越多。建议值:距离=0;精度=0。从LocationSensor组件读取位置信息(纬度、经度)TLocationSensor组件需要激活才能使用。您可以根据输入(如TSwitch组件或其他应用程序事件)打开/关闭TLocationSensor。

从工具选项板中放置一个TLocationSensor组件。在表单设计器上,选择TSwitch组件。在对象检查器的"事件"选项卡中,双击"OnSwitch事件"。将以下代码添加到OnSwitch事件处理程序:

Delphi:

procedure TForm1.Switch1Switch(Sender: TObject);
begin
  LocationSensor1.Active := Switch1.IsChecked;
end;

C++:

void __fastcall TForm1::Switch1Switch(TObject *Sender)
{
        LocationSensor1->Active = Switch1->IsChecked;
}

如前所述,当您移动移动设备时,TLocationSensor会触发OnLocationChanged事件。您可以使用带有事件处理程序的参数来显示当前位置(纬度和经度)。

在表单设计器上,选择TLocationSensor。在对象检查器的事件选项卡中,双击OnLocationChange事件。将以下代码添加到OnLocationChange事件处理程序:Delphi:

procedure TForm1.LocationSensor1LocationChanged(Sender: TObject;
  const OldLocation, NewLocation: TLocationCoord2D);
var
  LDecSeparator: String;
begin
  LDecSeparator := FormatSettings.DecimalSeparator;
  FormatSettings.DecimalSeparator := '.';
  // Show current location
  ListBoxItemLatitude.ItemData.Detail  := Format('%2.6f', [NewLocation.Latitude]);
  ListBoxItemLongitude.ItemData.Detail := Format('%2.6f', [NewLocation.Longitude]);
end;

C++:

void __fastcall TForm1::LocationSensor1LocationChanged(TObject *Sender, const TLocationCoord2D &OldLocation,
                  const TLocationCoord2D &NewLocation)
{
        char LDecSeparator = FormatSettings.DecimalSeparator;
        FormatSettings.DecimalSeparator = '.';
        // Show current location
        ListBoxItemLatitude->ItemData->Detail = ListBoxItemLatitude->ItemData->Detail.sprintf(L"%2.6f", NewLocation.Latitude);
        ListBoxItemLongitude->ItemData->Detail = ListBoxItemLongitude->ItemData->Detail.sprintf(L"%2.6f", NewLocation.Longitude);
}

最新更新