I need to make a simple application for android that send the location of my smartphone every 25 seconds to a Web-app. My Web-app is online and right now i can pass the value manually like this:
http://mywebapp.com/coordinates/create?latitude=18.463108&longitude=-69.929117
I'm an absolute beginner to Android development, so try to explain me step by step.
Google Maps will present a small pop-up on the bottom of the screen with your location. Pull up on that pop-up menu. Next to a marker icon you'll see the Plus Code (above your longitude and latitude.) Copy and paste that and send it to a friend to give them your current location.
Share the location if you like. Click "Share" and then choose the messaging application you would like to use. Send the message or email to yourself or to a friend. The share will include the location's latitude and longitude.
After doing a deep research, I found what I think is the easiest way to send GPS location (Latitude,Longitude) in a query string. Maybe there's no shorter code in the web than this. So we'll do this in 2 steps:
First add this permissions to the AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Now the code in your MainActivity.java
inside the class public class MainActivity extends Activity { }
public class MainActivity extends Activity {
public double latitude;
public double longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
String Text = "My current Latitude = " + latitude + " Longitude = " + longitude;
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
SendQueryString(); // for send the Query String of latitude and logintude to the webapp.
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}/* End of Class MyLocationListener */
public void SendQueryString() {
new Thread() {
public void run() {
String url = "http://mywebapp.com/coordinates/create?latitude=" + latitude +"&longitude=" + longitude;
try {
HttpClient Client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
Client.execute(httpget);
}
catch(Exception ex) {
String fail = "Fail!";
Toast.makeText( getApplicationContext(),fail,Toast.LENGTH_SHORT).show();
}
}
}.start();
}
}
NOTE: As you can see I'm using new Thread() { public void run() {/*code here*/}}.start();
in the function SendQueryString()
, that's because we're trying to make a request to a server, so in Android you can't make a request to a server in the principal Thread, where the graphics and principal process are. In fact it'll return an error if you try to make a request in the principal Thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With