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

Display User Location in Android App

The document provides a code implementation for an Android application that displays the user's current location using the FusedLocationProviderClient. It includes XML layout for the user interface and Java code for handling location permissions, retrieving the last known location, and updating the UI with latitude and longitude values. Additionally, it outlines the necessary permissions in the Android manifest file for location access.

Uploaded by

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

Display User Location in Android App

The document provides a code implementation for an Android application that displays the user's current location using the FusedLocationProviderClient. It includes XML layout for the user interface and Java code for handling location permissions, retrieving the last known location, and updating the UI with latitude and longitude values. Additionally, it outlines the necessary permissions in the Android manifest file for location access.

Uploaded by

pranjaliingawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PRACTICAL 31

Develop a program to display user’s current location:

Xml code

<?xml version="1.0" encoding="utf-8"?> import [Link];


<LinearLayout import [Link];
xmlns:android="[Link] import [Link];
android:layout_width="match_parent" import [Link];
android:layout_height="match_parent" import [Link];
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" [Link](savedInstanceState);
android:text="Latitude will be here! " setContentView([Link].activity_main);
android:textColor="#f5f5f5" /> latitudeTextView = findViewById([Link]);
<TextView longitTextView = findViewById([Link]);
android:layout_width="wrap_content" mFusedLocationClient =
android:layout_height="wrap_content" [Link](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 [Link].prac31;
[Link]().addOnCompleteListener(
import [Link];
new OnCompleteListener<Location>() {
import [Link];
@Override
import [Link];
public void onComplete(@NonNull Task<Location>
import [Link];
task) {
import [Link];
Location location = [Link]();
import [Link];
if (location == null) {
import [Link];
requestNewLocationData();
import [Link];
} else {
import [Link];
[Link]([Link]() +
import [Link];
"");
import [Link];
[Link]([Link]() +
import [Link];
"");
import [Link];
}
import [Link];
}
import [Link];
});
[Link];
} else {
import [Link];
PRACTICAL 31

[Link](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); // [Link](this,
startActivity(intent); [Link].ACCESS_BACKGROUND_LOCATION) ==
} PackageManager.PERMISSION_GRANTED
} else { }
// if permissions aren't available,
// request for permissions // method to request for permissions
requestPermissions(); private void requestPermissions() {
} [Link](this, new String[]{
} [Link].ACCESS_COARSE_LOCATION,
[Link].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)
[Link](LocationRequest.PRIORITY_HIGH_A getSystemService(Context.LOCATION_SERVICE);
CCURACY); return
[Link](5); [Link](LocationManager.GPS_PROVI
[Link](0); DER) ||
[Link](1); [Link](LocationManager.NETWORK_
PROVIDER);
// setting LocationRequest }
// on FusedLocationClient
mFusedLocationClient = // If everything is alright then
[Link](this); @Override
public void
[Link](mLocationReques onRequestPermissionsResult(int requestCode, @NonNull
t, mLocationCallback, [Link]()); String[] permissions, @NonNull int[] grantResults) {
} [Link](requestCode,
permissions, grantResults);
private LocationCallback mLocationCallback = new
LocationCallback() { if (requestCode == PERMISSION_ID) {
if ([Link] > 0 && grantResults[0] ==
@Override PackageManager.PERMISSION_GRANTED) {
public void onLocationResult(LocationResult locationResult) { getLastLocation();
Location mLastLocation = [Link](); }
[Link]("Latitude: " + }
[Link]() + ""); }
[Link]("Longitude: " +
[Link]() + ""); @Override
} public void onResume() {
}; [Link]();
if (checkPermissions()) {
// method to check for permissions getLastLocation();
private boolean checkPermissions() { }
return [Link](this, }
[Link].ACCESS_COARSE_LOCATION) == }
PackageManager.PERMISSION_GRANTED &&
[Link](this,
[Link].ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED;
PRACTICAL 31

Manifest code Outputs


<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="[Link]
xmlns:tools="[Link]
package="[Link].prac31">
<uses-permission
android:name="[Link].ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name="[Link].ACCESS_FINE_LOCATION" />
<uses-permission
android:name="[Link].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="[Link]" />
<category
android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>

You might also like