如何使用delphi将应用程序重定向到android中的位置传感器设置

时间:2016-09-14 19:04:37

标签: android delphi firemonkey delphi-10.1-berlin

我使用Delphi 10.1 Berlin开发Android移动应用程序。并且我需要检查位置传感器是否未打开,然后我需要重定向到android mobile中的位置传感器设置。我怎样才能使用Delphi实现?我见过使用JAVA的例子但是没有找到Delphi的例子。并提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码。这是一个帮手单位:

unit LocationU;

interface

function IsGPSProviderEnabled: Boolean;

function IsNetworkProviderEnabled: Boolean;

procedure LaunchLocationSettings;

implementation

uses
  System.SysUtils,
  Androidapi.Helpers,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Location,
  Androidapi.JNI.Provider;

function IsProviderEnabled(const Provider: JString): Boolean;
var
  LocationManagerObj: JObject;
  LocationManager: JLocationManager;
begin
  LocationManagerObj := TAndroidHelper.Context.getSystemService(
    TJContext.JavaClass.LOCATION_SERVICE);
  LocationManager := TJLocationManager.Wrap(LocationManagerObj);
  Result := LocationManager.isProviderEnabled(Provider);
end;

function IsGPSProviderEnabled: Boolean;
begin
  Result := IsProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
end;

function IsNetworkProviderEnabled: Boolean;
begin
  Result := IsProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER);
end;

procedure LaunchLocationSettings;
begin
  TAndroidHelper.Activity.startActivity(
    TJIntent.JavaClass.init(TJSettings.JavaClass.ACTION_LOCATION_SOURCE_SETTINGS));
end;

end.

以下是一些调用它的代码:

uses
  FMX.Helpers.Android,
  Androidapi.Helpers,
  Androidapi.JNI.Widget,
  LocationU;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  if not IsGPSProviderEnabled and not IsNetworkProviderEnabled then
  begin
    CallInUiThread(
      procedure
      begin
        TJToast.JavaClass.makeText(
          TAndroidHelper.Context,
          StrToJCharSequence('Location services not enabled - launching settings'),
          TJToast.JavaClass.LENGTH_SHORT).show
      end);
    LaunchLocationSettings;
  end;
end;