Hello friends
Today i am gonna to discuss about taking raw data or coordinate from an android mobile GPS device.
Let's Start................
Just enter into programming mode in android studio.
If you don't know that how to do that please read this post.
LocationManager lm;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
return START_STICKY;
}
@Override
public void onLocationChanged(Location location) {
try{
Toast.makeText(this, "GPS Values Found: " + String.valueOf(location.getLongitude()) + " : " + String.valueOf(location.getLatitude()), Toast.LENGTH_SHORT).show();
}catch(Exception ex){
Log.i("Error in getting location valus", ex.getMessage());
}
}
@Override
public void onProviderDisabled(String provider) {
/* this is called if/when the GPS is disabled in settings */
/* bring up the GPS settings */
//Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//startActivity(intent);
Toast.makeText(this, "GPS Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
/* This is called when the GPS status alters */
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
Toast.makeText(this, "Status Changed: Out of Service",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Toast.makeText(this, "Status Changed: Temporarily Unavailable",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.AVAILABLE:
Toast.makeText(this, "Status Changed: Available",
Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<service android:name=".BootService"></service>
Today i am gonna to discuss about taking raw data or coordinate from an android mobile GPS device.
Let's Start................
Step:1
Just enter into programming mode in android studio.
If you don't know that how to do that please read this post.
Step:2
Now just make a new class as show in figure and name it BootService.
Step:3
Now extends Service and implements LocationListener as shown in figure below.
Step:4
Now just copy and paste following code into BootService class.
LocationManager lm;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
return START_STICKY;
}
@Override
public void onLocationChanged(Location location) {
try{
Toast.makeText(this, "GPS Values Found: " + String.valueOf(location.getLongitude()) + " : " + String.valueOf(location.getLatitude()), Toast.LENGTH_SHORT).show();
}catch(Exception ex){
Log.i("Error in getting location valus", ex.getMessage());
}
}
@Override
public void onProviderDisabled(String provider) {
/* this is called if/when the GPS is disabled in settings */
/* bring up the GPS settings */
//Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//startActivity(intent);
Toast.makeText(this, "GPS Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
/* This is called when the GPS status alters */
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
Toast.makeText(this, "Status Changed: Out of Service",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Toast.makeText(this, "Status Changed: Temporarily Unavailable",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.AVAILABLE:
Toast.makeText(this, "Status Changed: Available",
Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
Step:5
Now Just copy and paste following permissions into manifest file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<service android:name=".BootService"></service>
Step:6
Now just connect your mobile phone to pc via USB cable and click on run in android studio.This will install app into your mobile,where you can see GPS values as toast message.
IF YOU HAVE ANY QUERY PLEASE COMMENT BELOW
Comments
Post a Comment