Enable/Disable GPS via Delphi

Asked

Viewed 2,033 times

0

There’s some specific code to enable the GPS, but I haven’t found it yet.

What I saw online was:

Post na Stack - On/Off GPS

however, this code is in JAVA.

Whatever he could ride 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 fourth was:

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

All of them apparently require permission to enable and deal directly with the android Settings.

The error I got was (in all):

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

In my options, permissions are normally enabled:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Not to mention I also tried to put the permissions directly in the Android Manifest.

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

Does anyone have any tips for me?

  • Ramon, you wouldn’t have to wear one JString within the setClassName?

  • Wakim, follow the full post. Please take a look :D

  • Ramon, this permission is only for system apps or firmware, you’ll have to see another way. I never used Delphi to develop for Android, I’ll try to search something to see if it helps.

  • No way to change the firmware via code? according to my research, it was to have worked from the moment I set true in WRITE_SECURE_SETTINGS.

  • No no, this code even works, but only apps that belong to the system or are firmware apps (made by device manufacturers) can have this permission (WRITE_SECURE_SETTINGS). I have an app that enables and disables the GPS, so there must be some other way.

  • But your app is in Delphi or Java?

  • Having an app is to have downloaded on Google Play. The app is the Extended Settings. It enables and disables gps. So you should have an alternative.

  • Surely there must be, I must be asking is the wrong question.

  • Wait a little longer, someone must know how to do this.

  • therefore I searched, there is no solution to activate/deactivate GPS programmatically, it is a security flaw (https://code.google.com/p/android/issues/detail?id=35924) that was corrected in version 4.4 only, but there is a code that can work (I am not sure) with API smaller than 4.4. The app I told you about (Settings Extended) redirects me to the configuration of Location in the device settings. I made this Gist for the two examples: https://gist.github.com/wakim/3cf3c31ed39048bd7b50. They are in Java, but you can try to convert to Delphi.

  • Anyone else has any tips to give?

Show 6 more comments

1 answer

3

I know the doubt is old but as I spent work with it too, follows 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;
  • Welcome to Sopt @Evandro! It would be nice if you add comments in your code explaining how it works, it would help a lot whoever is implementing your code :DD

  • Damn man! Thank you very much, the ages I try to do this! Now, just a question @Evandro, this solution would work on XE5?

  • Ramon, the reference I searched was in a Korean developer who at the time used XE5.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.