0% found this document useful (0 votes)
5 views

ANDROID LOCATION BUT BUTTON IS THER

The document is an Android activity class that manages location updates using GPS. It requests location permissions and displays the user's latitude and longitude on the screen when a button is clicked. The activity also handles the removal of location updates when paused to conserve resources.

Uploaded by

vllthag
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)
5 views

ANDROID LOCATION BUT BUTTON IS THER

The document is an Android activity class that manages location updates using GPS. It requests location permissions and displays the user's latitude and longitude on the screen when a button is clicked. The activity also handles the removal of location updates when paused to conserve resources.

Uploaded by

vllthag
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

import android.

Manifest;

import android.content.Context;

import android.content.pm.PackageManager;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;

import androidx.core.content.ContextCompat;

public class LocationActivity extends AppCompatActivity {

private LocationManager locationManager;

private LocationListener locationListener;

private TextView locationTextView;

private Button getLocationButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);

locationTextView = findViewById(R.id.tv_location);

getLocationButton = findViewById(R.id.btn_get_location);

// LocationListener to get location updates

locationListener = new LocationListener() {

@Override

public void onLocationChanged(Location location) {

// Display the location on the TextView

double latitude = location.getLatitude();

double longitude = location.getLongitude();

locationTextView.setText("Latitude: " + latitude + "\nLongitude: " + longitude);

@Override

public void onStatusChanged(String provider, int status, Bundle extras) {}

@Override

public void onProviderEnabled(String provider) {}

@Override

public void onProviderDisabled(String provider) {}

};

// Set up the Button click listener


getLocationButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// Check if permissions are granted

if (ContextCompat.checkSelfPermission(LocationActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)

!= PackageManager.PERMISSION_GRANTED &&

ContextCompat.checkSelfPermission(LocationActivity.this,
Manifest.permission.ACCESS_COARSE_LOCATION)

!= PackageManager.PERMISSION_GRANTED) {

// If permissions are not granted, request them

ActivityCompat.requestPermissions(LocationActivity.this,

new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, 1);

} else {

// If permissions are granted, get location updates

requestLocationUpdates();

});

private void requestLocationUpdates() {

// Use GPS provider

String provider = LocationManager.GPS_PROVIDER; // or NETWORK_PROVIDER

try {
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);

} catch (SecurityException e) {

e.printStackTrace();

@Override

protected void onPause() {

super.onPause();

// Remove location updates when not needed

locationManager.removeUpdates(locationListener);

You might also like