Android Question Turn on GPS

Discussion in 'Android Questions' started by jefflynn1974, Feb 19, 2015.

Similar threads

B4A Question turn on gps automatically in B4A
B4A Question How to match a GPS back turn ?
B4A Tutorial GPS tutorial
B4A Tutorial MJPEG / CCTV Server
B4A Question GPS when display turn off
  1. jefflynn1974

    jefflynn1974 Member Licensed User

  2. eurojam

    eurojam Well-Known Member Licensed User

    lemonisdead likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    Here:
    Code:
    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).
     
    eurojam and lemonisdead like this.
  4. jefflynn1974

    jefflynn1974 Member Licensed User

    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.

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

    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
     
  6. jefflynn1974

    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?
     
  7. eurojam

    eurojam Well-Known Member Licensed User

    Jeff,
    i've tried it on a non rooted nexus5 with 5.0.1

    stefan
     
  8. jefflynn1974

    jefflynn1974 Member Licensed User

    Thanks for the info! It seems this is not working on 4.4 and above unfortunately...
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice