Android Question Turn on GPS

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim NativeMe As JavaObject
   NativeMe.InitializeContext
   NativeMe.RunMethod("turnGPSOn", Null)
End Sub
#If JAVA
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
public void turnGPSOn()
{
  Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
  intent.putExtra("enabled", true);
  this.sendBroadcast(intent);

  String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
  if(!provider.contains("gps")){ //if gps is disabled
  final Intent poke = new Intent();
  poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
  poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
  poke.setData(Uri.parse("3"));
  this.sendBroadcast(poke);


  }
}
#End IF

It doesn't work (as expected).
 
Upvote 0

jefflynn1974

Member
Licensed User
Longtime User
Did you read the Threads in this forum about this like:
https://www.b4x.com/android/forum/threads/toggle-on-off-gps-in-any-android-version.44718/ (and see the remark from Erel!)
and
https://www.b4x.com/android/forum/threads/togglelibrary.12388/

best reards
stefan

Of course I read. The first method needs root and the second one is not working on newer devices.

At my link's code they say that it works and no mention about root.

Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim NativeMe As JavaObject
   NativeMe.InitializeContext
   NativeMe.RunMethod("turnGPSOn", Null)
End Sub
#If JAVA
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
public void turnGPSOn()
{
  Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
  intent.putExtra("enabled", true);
  this.sendBroadcast(intent);

  String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
  if(!provider.contains("gps")){ //if gps is disabled
  final Intent poke = new Intent();
  poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
  poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
  poke.setData(Uri.parse("3"));
  this.sendBroadcast(poke);


  }
}
#End IF

It doesn't work (as expected).

Wow, you were so fast Erel! :D Thanks, I will try it too.
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
I have also tried Erels code, which is the code from your stackoverflow thread as inline java code - that is a realy awesome method in the new b4a!! - but it didn't work. I think the device must be also rooted. The log say's something like this: Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=21326, uid=10107
 
Upvote 0

jefflynn1974

Member
Licensed User
Longtime User
I checked the method on some devices and results are mixed. It worked on rooted phones with 4.0.3 and 4.3. There was a positive result too on a 4.1.2 without root and a negative on 4.4.2. Last one give an error code: "An error has occured in sub: java.lang.reflect.InvocationTargetException" I think it is a different error than Eurojam's, isn't it?

Eurojam, on which version have you tried?
 
Upvote 0
Top