Android Question Turn on GPS

Erel

Administrator
Staff member
Licensed 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).
 

jefflynn1974

Member
Licensed User
Did you read the Threads in this forum about this like:
http://www.b4x.com/android/forum/threads/toggle-on-off-gps-in-any-android-version.44718/ (and see the remark from Erel!)
and
http://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.
 

eurojam

Well-Known Member
Licensed 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
 

jefflynn1974

Member
Licensed 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?
 
Top