Q.5) What is Location Provider? Explain its usage in Android with a suitable example.Ans.LocationProvider is the super class for location providers. A location provider provides the information of the geographical location of the device. This information is stored in the Location class.
For accessing the location based services Android provide different classes that are available in android. location.
The different classes are as follows:
You can initialize LocationManager object according to given below code.
LocationManager service = (LocationManager)getSystemService(LOCATION_SERVICE);EXAMPLE:Create a new project by File-> New -> Android Project
Give the project name as Location. In the given below example create a separate class name it LocationService. I have access this class in MainActivity.java class.
After creating the classes your Android workspace folder structure will look like as given below.
Code for MainActivity.java classpackage com.example.location;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btnLocation;
LocationService appService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appService = new LocationService(this);
btnLocation = (Button) findViewById(R.id.btnGPSLocation);
btnLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Location gpsLocation = appService.getLocation(LocationManager.GPS_PROVIDER);
if (gpsLocation != null) {
double latitude = gpsLocation.getLatitude();
double longitude = gpsLocation.getLongitude();
Toast.makeText(getApplicationContext(),"Mobile Location (GPS): \nLatitude: " + latitude+ "\nLongitude: " + longitude,Toast.LENGTH_LONG).show();
} else
{
showAlertDialog("GPS Service");
}
}
});
}
public void showAlertDialog(String title) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle(title + " SETTINGS");
alertDialog.setMessage(title + " is not enabled! Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
alertDialog.show();
}
}
Code for LocationService.java class
package com.example.location;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
public class LocationService extends Service implements LocationListener{
protected LocationManager locationManager;
Location location;
private static final long MIN_UPDATE_TIME = 1000 * 60 * 3;
public LocationService(Context context)
{
locationManager = (LocationManager)context.getSystemService(LOCATION_SERVICE);
}
public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider))
{
locationManager.requestLocationUpdates(provider,MIN_UPDATE_TIME, 10, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
return location;
}
}
return null;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Write given below code in AndroidManifest.xml after uses-sdk tag.
< !-- to get location using GPS -->
< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
< uses-permission android:name="android.permission.INTERNET" />
Run the application you will get the result as given below image.