Firemonkey为Android启用GPS服务

 2023-01-20    360  

问题描述

我在徘徊,如果您可以在Firemonkey项目中启用GPS服务?
位置传感器不启用它.如果启用GP或您有网络位置,它只能获得GPS坐标.

在其他一些Android应用程序中,它询问您是否要启用GPS,如果您同意,它将启用该应用程序的GPS.我也想这样做.

Firemonkey为Android启用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/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;

其他推荐答案

位置传感器
位置传感器由tlocationsentor组件包裹.

当设备检测运动时,

tlocationsentor会发射洋关节的事件.您可以使用距离和准确性属性调整tlocationsentor的灵敏度.

距离属性指定设备必须移动的最小距离(以米为单位),以使位置传感器重新安置设备并返回新的位置信息.例如,如果将距离设置为” 10″,则当您移动” 10米”时,tlocationsensor会发射on LocationChanged事件.
准确性属性代表传感器在地理位置上定位设备的精度(以米为单位)的级别,相对于设备实际位置的地理点.
提示:您应该指定适合您应用程序的最低精度;准确性越高,传感器确定位置所需的时间和功率就越多.推荐值:距离= 0;精度= 0.
读取位置信息组件的位置信息(纬度,经度)
需要激活tlocationsentor组件以供使用.您可以根据输入(例如TSWitch组件或其他应用程序事件)打开/关闭tlocationsEnsor.

从工具调色板上放置一个tlocationsentor组件.
在表单设计器上,选择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发射了一个启用的事件.您可以使用带有事件处理程序的参数显示当前位置(纬度和经度).

在表单设计器上,选择tlocationsensor.
在”对象检查器”中,在”事件”选项卡中双击OnLocationChange事件.
将以下代码添加到OnLocation Change事件处理程序:
德尔菲:

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);
}

以上所述是小编给大家介绍的Firemonkey为Android启用GPS服务,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/26100.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。