Assignment 6 Location Based Services
Assignment 6 Location Based Services
Android Location-Based Services (LBS) enable apps to obtain and use geographical
location data, which can be used to enhance app functionality and provide personalized
experiences to users. Here's an overview of Location-Based Services in Android:
Key Concepts
1. Location Providers:
○ GPS Provider: Uses GPS satellites to determine the location, which is highly
accurate but consumes more power.
○ Network Provider: Uses cellular and Wi-Fi networks to estimate the location,
less accurate but faster and consumes less battery.
○ Passive Provider: Uses the location updates from other applications or
services, reducing battery consumption.
2. Geocoding:
● Combines location signals from GPS, Wi-Fi, and mobile networks to provide more
accurate and efficient location data.
● Reduces battery consumption by intelligently choosing the best available location
provider.
Key Components
LocationManager:
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Permissions:
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
package com.example.tutorialspoint7.myapplication;
import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.test.mock.MockPackageManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Button btnShowLocation;
// GPSTracker class
GPSTracker gps;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
if (ActivityCompat.checkSelfPermission(this, mPermission)
!= MockPackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{mPermission},
REQUEST_CODE_PERMISSION);
// If any permission above not allowed by user, this
condition will
} catch (Exception e) {
e.printStackTrace();
btnShowLocation.setOnClickListener(new View.OnClickListener()
{
@Override
if(gps.canGetLocation()){
Toast.makeText(getApplicationContext(), "Your
Location is - \nLat: "
}else{
gps.showSettingsAlert();
});
GPSTracker.java.
package com.example.tutorialspoint7.myapplication;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
this.mContext = context;
getLocation();
try {
locationManager = (LocationManager)
mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled =
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
} catch (Exception e) {
e.printStackTrace();
}
return location;
/**
* */
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
/**
* */
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
/**
* */
if(location != null){
longitude = location.getLongitude();
// return longitude
return longitude;
/**
* @return boolean
* */
return this.canGetLocation;
}
/**
* */
alertDialog.setTitle("GPS is settings");
alertDialog.setPositiveButton("Settings", new
DialogInterface.OnClickListener() {
mContext.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
dialog.cancel();
});
alertDialog.show();
@Override
@Override
@Override
}
@Override
@Override
return null;
<LinearLayout xmlns:android =
"http://schemas.android.com/apk/res/android"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
<Button
android:id = "@+id/button"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "getlocation"/>
</LinearLayout>
<resources>
</resources>
<manifest xmlns:android =
"http://schemas.android.com/apk/res/android"
package = "com.example.tutorialspoint7.myapplication">
<uses-permission android:name =
"android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup = "true"
android:icon = "@mipmap/ic_launcher"
android:label = "@string/app_name"
android:supportsRtl = "true"
android:theme = "@style/AppTheme">
<activity android:name = ".MainActivity">
<intent-filter>
<category android:name =
"android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>