0% found this document useful (0 votes)
18 views2 pages

Practical No 31

The document contains Java code for an Android application that retrieves the user's current location using the Fused Location Provider. It checks for location permissions and displays the latitude and longitude in a TextView when a button is clicked. If the location cannot be retrieved, it shows a toast message indicating the failure.

Uploaded by

shaikhsamiaa
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)
18 views2 pages

Practical No 31

The document contains Java code for an Android application that retrieves the user's current location using the Fused Location Provider. It checks for location permissions and displays the latitude and longitude in a TextView when a button is clicked. If the location cannot be retrieved, it shows a toast message indicating the failure.

Uploaded by

shaikhsamiaa
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/ 2

Practical no 31

// MainActivity.java

package com.example.locationapp;

import android.Manifest;

import android.content.pm.PackageManager;

import android.location.Location;

import android.os.Bundle;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;

import com.google.android.gms.location.FusedLocationProviderClient;

import com.google.android.gms.location.LocationServices;

import com.google.android.gms.tasks.OnSuccessListener;

public class MainActivity extends AppCompatActivity {

private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;

private FusedLocationProviderClient fusedLocationClient;

private TextView locationTextView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

locationTextView = findViewById(R.id.locationTextView);

Button getLocationButton = findViewById(R.id.getLocationButton);

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

getLocationButton.setOnClickListener(v -> getCurrentLocation());

private void getCurrentLocation() {

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&

ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);

return;}

fusedLocationClient.getLastLocation()

.addOnSuccessListener(this, new OnSuccessListener<Location>() {

@Override

public void onSuccess(Location location) {

if (location != null) {

String locationText = "Latitude: " + location.getLatitude() + "\nLongitude: " + location.getLongitude();

locationTextView.setText(locationText);

} else {

Toast.makeText(MainActivity.this, "Unable to get location", Toast.LENGTH_SHORT).show();

});}

@Override

You might also like