Enable / Disable GPS via Delphi

0

There is some specific code to enable GPS, but I have not found it yet.

What I saw online was:

  

Post on Stack - On / Off GPS

However, this code is in JAVA.

What I was able to mount in Delphi was:

var
  poke : JIntent;
begin
  poke.setClassName(StringToJString('com.android.settings'),StringToJString('com.android.settings.widget.SettingsAppWidgetProvider'));
  poke.addCategory(StringToJString('android.intent.category.ALTERNATIVE'));
  poke.setData(TJnet_Uri.JavaClass.parse(StringToJString('3')));
  SharedActivity.sendBroadcast(poke);
end;

Second code I got to activate is:

TJSettings_Secure.JavaClass.putString(SharedActivityContext.getContentResolver,
                                      TJSettings_Secure.JavaClass.LOCATION_PROVIDERS_ALLOWED,
                                      TJLocationManager.JavaClass.GPS_PROVIDER);

Third was:

TJSettings_Secure.JavaClass.setLocationProviderEnabled(
  SharedActivityContext.getContentResolver,
  TJLocationManager.JavaClass.GPS_PROVIDER, 
  true);

The room was:

Intent := TJIntent.JavaClass.init(TJLocationManager.JavaClass.GPS_PROVIDER);
  intent.putExtra(StringToJString('enabled'), true);
  SharedActivity.sendBroadcast(intent);

They all need, apparently permission to enable and deal directly with the android settings.

The error I got was (at all):

java.lang.SecurityException: Permission denial: writing to secure settings requires android.permission.WRITE_SECURE_SETTINGS

In my options, permissions are usually enabled:

NottomentionthatIalsotriedtoputthepermissionsdirectlyontheAndroidManifest.

<uses-permissionandroid:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

Does anyone have any tips to give me?

    
asked by anonymous 22.09.2014 / 13:40

1 answer

3

I know the doubt is old but as I worked on it too, there is a solution. I use Delphi XE7. Basically when I click the button it opens the screen to enable Android GPS.

Uses
Androidapi.JNI.JavaTypes,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Location,
  Androidapi.JNIBridge,
  Androidapi.JNI.Os,
  Androidapi.Helpers,
  Androidapi.JNI.Provider;
.............................
procedure TForm2.Button6Click(Sender: TObject);
var
  Provider:string;
  Settings_secure:TJSettings_Secure;
  Intent:JIntent;
begin
  Provider:=JStringToString(Settings_secure.JavaClass.getString(
  SharedActivityContext.getContentResolver,
  TJSettings_system.javaClass.LOCATION_PROVIDERS_ALLOWED));
// if pos(‘gps‘,provider)=0 then
// begin
    Intent:=TJIntent.Create;
    Intent.setAction(TJSettings.javaClass.ACTION_LOCATION_SOURCE_SETTINGS);
    SharedActivity.startActivity(Intent);
// end;
end;
    
11.09.2015 / 15:23