0% found this document useful (0 votes)
10 views4 pages

Practical No. 31

The document provides code for an Android application that fetches the user's current location using the Fused Location Provider. It includes XML layout for the main activity with a button to trigger location fetching and a TextView to display the coordinates. The Java code handles permission requests and retrieves location data, displaying it on the screen if granted.

Uploaded by

ganeshkumbhar638
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)
10 views4 pages

Practical No. 31

The document provides code for an Android application that fetches the user's current location using the Fused Location Provider. It includes XML layout for the main activity with a button to trigger location fetching and a TextView to display the coordinates. The Java code handles permission requests and retrieves location data, displaying it on the screen if granted.

Uploaded by

ganeshkumbhar638
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/ 4

Q.

Write a program to locate user’s current import


location. androidx.appcompat.app.AppCompatActivity
;
activity_main.xml import androidx.core.app.ActivityCompat;

<?xml version="1.0" encoding="utf-8"?> import android.Manifest;


<!-- activity_main.xml --> import android.content.pm.PackageManager;
<LinearLayout import android.location.Location;
import android.os.Bundle;
xmlns:android="http://schemas.android.com/ import android.view.View;
apk/res/android" import android.widget.Button;
android:layout_width="match_parent" import android.widget.TextView;
android:layout_height="match_parent" import android.widget.Toast;
android:orientation="vertical"
android:background="@color/white" import
android:padding="16dp" com.google.android.gms.location.FusedLocat
android:gravity="center"> ionProviderClient;
import
<Button com.google.android.gms.location.LocationSe
android:id="@+id/btn" rvices;
android:layout_width="wrap_content" import
android:layout_height="60dp" com.google.android.gms.tasks.OnSuccessList
android:text="Fetch My Current ener;
Location..."
android:textSize="20sp" /> public class MainActivity extends
AppCompatActivity {
<TextView
android:id="@+id/txt" private static final int
android:layout_width="wrap_content" PERMISSION_REQUEST_CODE = 100;
android:layout_height="60dp" private FusedLocationProviderClient
android:layout_marginTop="20dp" fusedLocationProviderClient;
android:textSize="20sp" /> private Button btn;
private TextView txt;
</LinearLayout>
@Override
MainActivity.java protected void onCreate(Bundle
package com.example.practicalno31; savedInstanceState) {
super.onCreate(savedInstanceState);
import androidx.annotation.NonNull; setContentView(R.layout.activity_main);

btn = findViewById(R.id.btn);
txt = findViewById(R.id.txt); .addOnSuccessListener(new
fusedLocationProviderClient = OnSuccessListener<Location>() {
LocationServices.getFusedLocationProvider @Override
Client(this); public void onSuccess(Location
location) {
btn.setOnClickListener(new if (location != null) {
View.OnClickListener() { txt.setText("Latitude: " +
@Override location.getLatitude() +
public void onClick(View v) { "\nLongitude: " +
if location.getLongitude());
(ActivityCompat.checkSelfPermission(Main } else {
Activity.this,
Toast.makeText(MainActivity.this, "Unable
Manifest.permission.ACCESS_FINE_LOCA to get location",
TION) Toast.LENGTH_SHORT).show();
!= }
PackageManager.PERMISSION_GRANTED }
){ });
}
ActivityCompat.requestPermissions(MainAct
ivity.this, @Override
new public void onRequestPermissionsResult(int
String[]{Manifest.permission.ACCESS_FIN requestCode, @NonNull String[]
E_LOCATION}, permissions,
@NonNull int[]
PERMISSION_REQUEST_CODE); grantResults) {
} else {
getCurrentLocation(); super.onRequestPermissionsResult(requestC
} ode, permissions, grantResults);
Toast.makeText(MainActivity.this, if (requestCode ==
"Fetching your Current Location..!", PERMISSION_REQUEST_CODE &&
Toast.LENGTH_SHORT).show(); grantResults.length > 0
} && grantResults[0] ==
}); PackageManager.PERMISSION_GRANTED
} ){
getCurrentLocation();
private void getCurrentLocation() { } else {
Toast.makeText(this, "Permission
fusedLocationProviderClient.getLastLocation Denied!", Toast.LENGTH_SHORT).show();
() }
}
} <action
android:name="android.intent.action.MAIN"
Androidmanifeast.xml />

<?xml version="1.0" encoding="utf-8"?> <category


<manifest android:name="android.intent.category.LAU
xmlns:android="http://schemas.android.com/ NCHER" />
apk/res/android" </intent-filter>
</activity>
xmlns:tools="http://schemas.android.com/too </application>
ls">
</manifest>
<uses-permission
android:name="android.permission.ACCESS
_FINE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS
_COARSE_LOCATION" />

<application
android:allowBackup="true"

android:dataExtractionRules="@xml/data_ex
traction_rules"

android:fullBackupContent="@xml/backup_
rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_
round"
android:supportsRtl="true"

android:theme="@style/Theme.PracticalNo3
1"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>

You might also like