0% found this document useful (0 votes)
6 views3 pages

Practical 31

The document outlines a program that displays the user's current location using Android's FusedLocationProviderClient. It includes XML code for the user interface and Java code for handling location requests, permissions, and displaying latitude and longitude. The manifest file specifies the necessary permissions for accessing location data.

Uploaded by

st5617067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Practical 31

The document outlines a program that displays the user's current location using Android's FusedLocationProviderClient. It includes XML code for the user interface and Java code for handling location requests, permissions, and displaying latitude and longitude. The manifest file specifies the necessary permissions for accessing location data.

Uploaded by

st5617067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PRACTICAL 31

Develop a program to display user’s current location:

Xml code

<?xml version="1.0" encoding="utf-8"?> import com.google.android.gms.location.LocationRequest;


<LinearLayout import com.google.android.gms.location.LocationResult;
xmlns:android="http://schemas.android.com/apk/res/android" import com.google.android.gms.location.LocationServices;
android:layout_width="match_parent" import com.google.android.gms.tasks.OnCompleteListener;
android:layout_height="match_parent" import com.google.android.gms.tasks.Task;
android:background="#4caf50" public class MainActivity extends AppCompatActivity {
android:gravity="center" // initializing
android:orientation="vertical"> // FusedLocationProviderClient
<TextView // object
android:layout_width="wrap_content" FusedLocationProviderClient mFusedLocationClient;
android:layout_height="wrap_content" // Initializing other items
android:fontFamily="sans-serif-black" // from layout file
android:text="Latitude:" /> TextView latitudeTextView, longitTextView;
<TextView int PERMISSION_ID = 44;
android:id="@+id/latTextView" @Override
android:layout_width="wrap_content" protected void onCreate(Bundle savedInstanceState) {
android:layout_height="wrap_content" super.onCreate(savedInstanceState);
android:text="Latitude will be here! " setContentView(R.layout.activity_main);
android:textColor="#f5f5f5" /> latitudeTextView = findViewById(R.id.latTextView);
<TextView longitTextView = findViewById(R.id.lonTextView);
android:layout_width="wrap_content" mFusedLocationClient =
android:layout_height="wrap_content" LocationServices.getFusedLocationProviderClient(this);
android:fontFamily="sans-serif-black" // method to get the location
android:text="Longitude:" /> getLastLocation();
<TextView }
android:id="@+id/lonTextView" @SuppressLint("MissingPermission")
android:layout_width="wrap_content" private void getLastLocation() {
android:layout_height="wrap_content" // check if permissions are given
android:text="Longitude will be here! " if (checkPermissions()) {
android:textColor="#f5f5f5" /> // check if location is enabled
</LinearLayout> if (isLocationEnabled()) {
// getting last
// location from
// FusedLocationClient
Java code // object

package com.example.prac31;
mFusedLocationClient.getLastLocation().addOnCompleteListener(
import android.Manifest;
new OnCompleteListener<Location>() {
import android.annotation.SuppressLint;
@Override
import android.content.Context;
public void onComplete(@NonNull Task<Location>
import android.content.Intent;
task) {
import android.content.pm.PackageManager;
Location location = task.getResult();
import android.location.Location;
if (location == null) {
import android.location.LocationManager;
requestNewLocationData();
import android.os.Bundle;
} else {
import android.os.Looper;
latitudeTextView.setText(location.getLatitude() +
import android.provider.Settings;
"");
import android.widget.TextView;
longitTextView.setText(location.getLongitude() +
import android.widget.Toast;
"");
import androidx.annotation.NonNull;
}
import androidx.appcompat.app.AppCompatActivity;
}
import androidx.core.app.ActivityCompat;
});
importcom.google.android.gms.location.FusedLocationProviderClient;
} else {
import com.google.android.gms.location.LocationCallback;
PRACTICAL 31

Toast.makeText(this, "Please turn on" + " your // If we want background location


location...", Toast.LENGTH_LONG).show(); // on Android 10.0 and higher,
Intent intent = new // use:
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); // ActivityCompat.checkSelfPermission(this,
startActivity(intent); Manifest.permission.ACCESS_BACKGROUND_LOCATION) ==
} PackageManager.PERMISSION_GRANTED
} else { }
// if permissions aren't available,
// request for permissions // method to request for permissions
requestPermissions(); private void requestPermissions() {
} ActivityCompat.requestPermissions(this, new String[]{
} Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},
@SuppressLint("MissingPermission") PERMISSION_ID);
private void requestNewLocationData() { }

// Initializing LocationRequest // method to check


// object with appropriate methods // if location is enabled
LocationRequest mLocationRequest = new LocationRequest(); private boolean isLocationEnabled() {
LocationManager locationManager = (LocationManager)
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_A getSystemService(Context.LOCATION_SERVICE);
CCURACY); return
mLocationRequest.setInterval(5); locationManager.isProviderEnabled(LocationManager.GPS_PROVI
mLocationRequest.setFastestInterval(0); DER) ||
mLocationRequest.setNumUpdates(1); locationManager.isProviderEnabled(LocationManager.NETWORK_
PROVIDER);
// setting LocationRequest }
// on FusedLocationClient
mFusedLocationClient = // If everything is alright then
LocationServices.getFusedLocationProviderClient(this); @Override
public void
mFusedLocationClient.requestLocationUpdates(mLocationReques onRequestPermissionsResult(int requestCode, @NonNull
t, mLocationCallback, Looper.myLooper()); String[] permissions, @NonNull int[] grantResults) {
} super.onRequestPermissionsResult(requestCode,
permissions, grantResults);
private LocationCallback mLocationCallback = new
LocationCallback() { if (requestCode == PERMISSION_ID) {
if (grantResults.length > 0 && grantResults[0] ==
@Override PackageManager.PERMISSION_GRANTED) {
public void onLocationResult(LocationResult locationResult) { getLastLocation();
Location mLastLocation = locationResult.getLastLocation(); }
latitudeTextView.setText("Latitude: " + }
mLastLocation.getLatitude() + ""); }
longitTextView.setText("Longitude: " +
mLastLocation.getLongitude() + ""); @Override
} public void onResume() {
}; super.onResume();
if (checkPermissions()) {
// method to check for permissions getLastLocation();
private boolean checkPermissions() { }
return ActivityCompat.checkSelfPermission(this, }
Manifest.permission.ACCESS_COARSE_LOCATION) == }
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED;
PRACTICAL 31

Manifest code Outputs


<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.prac31">
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_BACKGROUND_LOCA
TION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Prac31">
<activity
android:name=".MainActivity"
android:exported="true"
tools:ignore="IntentFilterExportedReceiver">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

You might also like