[Question] GPS

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hey everyone!

I have (yet) another question.

I read somewhere that it is not possible to enable the GPS programmatically, but I searched some apps around the market and apparently, Lookout can do this:

https://market.android.com/details?id=com.lookout&feature=search_result

FIND MY PHONE

...
* Lookout can remotely enable GPS, so you can find your phone even if GPS is turned off
...

So now I guess it's possible?

Thanks a lot!

XverhelstX
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
Last edited:
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
pff, actually, I really don't get it.

something like this then?

B4X:
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

/**
 * 
 */
@ShortName("TurnGPS")
@Permissions(values={"android.permission.ACCESS_FINE_LOCATION"})
@Events(values={"TurnGPSOn (Enabled As Boolean)", "TurnGPSOff (Enabled As Boolean)" })
@Version(1f)

public class TurnGPS {
   
   private static void turnGPSOn(){
       String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

       if(!provider.contains("gps")){
           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")); 
           sendBroadcast(poke);
       }
   }


   private static void turnGPSOff(){
       String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

       if(provider.contains("gps")){
           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")); 
           sendBroadcast(poke);
       }
   }


}

that seems to work, but I need to create methods then for sendBroadcast and getContentResolver, but I really don't have a clue what to do then next.
and also how to get the TurnGPSon then let to work.


XverhelstX
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
As far as I can see you don't need anything other than the turnGPSOn and turnGpsOff methods. The method in your second link to the Android Issue posting seems to be doing the same as the StackOverflow code.

You don't need the @Events annotation. turnGPSOn and turnGpsOff don't raise any events. You could however add an @Author("My Name") annotation.

If you can get the code you posted compiled and exported as a jar with the matching xml file using the B4ADoclet it might just work - until the bug is fixed of course!
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
SendBroadcast and getContentResolver are Context class methds and so you need a Context or Activity object on which to invoke them. Basic4Android can give you one.

B4X:
Private BA ba
....
Public void Initialize(BA ba)
{
  this.ba = ba;
}

....
-----getString(ba.context.getContentResolver()------
...
ba.context.sendBroadcast(poke);
I haven't fully checked this but I got to rush and cook our evening meal :(
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
I don't get it.

So I have to make a new class named GPS.java(for example)


public class GPS

Private BA ba

Public void Initialize(BA ba)
{
this.ba = ba;
}
}

or where do I have to put this?

and what do i have to include with @ActivityObject?

thank you

xverhelstx
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
Oh now I understand it almost completely :eek: :D:D

I made my .jar and .xml and all, but I still encouter an error when compiling:

B4X:
Compiling generated Java code.          Error
B4A line: 119
End Sub
javac 1.6.0_21
src\xtremelyvirtualstudios\xvs\Tracked\main.java:257: cannot find symbol
symbol  : class TurnGPS
location: class xtremelyvirtualstudios.xvs.Tracked.main
TurnGPS _turnon = null;
^
1 error

B4X:
import android.content.Intent;
import android.net.Uri;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

/**
 * 
 */
@ShortName("TurnGPS")
@Permissions(values={"android.permission.WRITE_SECURE_SETTINGS"})
@ActivityObject
@Author("XverhelstX")
@Version(1f)


public class TurnGPS{
   
   private BA ba; 
   
   public void Initialize(BA ba)
   {
     this.ba = ba;
     
   }
    /**
     * Toggles the GPS. Note that this method actually activate the GPS immediately (unlike most other methods that
     *goes to the Settings Screen.).
     */
   public void turnGPS(){
       final Intent poke = new Intent();
       poke.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider"); //$NON-NLS-1$//$NON-NLS-2$
       poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
       poke.setData(Uri.parse("3")); //$NON-NLS-1$
       ba.context.sendBroadcast(poke);
   }

}

Thanks!

XverhelstX

EDIT; the problem on the prev page was that Private was with a capital letter.
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
Nope sorry ;p my "Thanks" was misleading :s.

So I haven't solved this problem yet and I still receive this error

Compiling generated Java code. Error
B4A line: 119
End Sub
javac 1.6.0_21
src\xtremelyvirtualstudios\xvs\Tracked\main.java:257: cannot find symbol
symbol : class TurnGPS
location: class xtremelyvirtualstudios.xvs.Tracked.main
TurnGPS _turnon = null;
^
1 error

when i try to compile.

I also tried to make a new one (another one, and I have the same error).

:S

XverhelstX
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
OK, your jar and xml files match but are for the wrong project. They both contain an AudioRecorder object so no wonder TurnGPS is not found. I suspect that you had the wrong source tree selected when you exported the jar and generated the xml. It's easily done in Eclipse.
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
oh, I don't think the problem is related to that.

because, while I was waiting, i accendently made this XML above the Audiorecorder one. but I already had the error before i did it.

I will retry.

Also note that I'm on Safe modus, because my pc is behaving very strange.

XverhelstX
 
Upvote 0
Top