Created
October 2, 2025 13:16
-
-
Save wilyJ80/429962937acc6bd8844de79b43d64ec2 to your computer and use it in GitHub Desktop.
Android geolocation permissions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:app="http://schemas.android.com/apk/res-auto" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:id="@+id/main" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| tools:context=".MainActivity"> | |
| <TextView | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="Location will be shown here!" | |
| app:layout_constraintBottom_toBottomOf="parent" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toTopOf="parent" | |
| android:id="@+id/textview" | |
| /> | |
| <Button | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintBottom_toBottomOf="parent" | |
| app:layout_constraintTop_toTopOf="@+id/textview" | |
| android:text="Get location" | |
| android:id="@+id/btn" | |
| /> | |
| </androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.myapplication; | |
| import android.Manifest; | |
| import android.content.Intent; | |
| import android.content.pm.PackageManager; | |
| import android.net.Uri; | |
| import android.os.Bundle; | |
| import android.provider.Settings; | |
| import android.widget.Button; | |
| import android.widget.TextView; | |
| import androidx.activity.EdgeToEdge; | |
| import androidx.annotation.NonNull; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| import androidx.core.app.ActivityCompat; | |
| import androidx.core.graphics.Insets; | |
| import androidx.core.view.ViewCompat; | |
| import androidx.core.view.WindowInsetsCompat; | |
| import com.google.android.gms.location.FusedLocationProviderClient; | |
| import com.google.android.gms.location.LocationServices; | |
| public class MainActivity extends AppCompatActivity { | |
| private FusedLocationProviderClient client; | |
| private TextView text; | |
| private static final int LOCATION_PERMISSION_REQUEST = 1001; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| text = findViewById(R.id.textview); | |
| Button btn = (Button) findViewById(R.id.btn); | |
| client = LocationServices.getFusedLocationProviderClient(this); | |
| btn.setOnClickListener(v -> getCurrentLocation()); | |
| } | |
| private void getCurrentLocation() { | |
| if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
| ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST); | |
| return; | |
| } | |
| client.getLastLocation().addOnSuccessListener(location -> { | |
| if (location != null) { | |
| double lat = location.getLatitude(); | |
| double lon = location.getLongitude(); | |
| text.setText("Latitude: " + lat + "\nLongitude: " + lon); | |
| } else { | |
| text.setText("Unable to get location"); | |
| } | |
| }); | |
| } | |
| // Handle the result of the permission request | |
| @Override | |
| public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
| super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
| // Permission denied | |
| if (requestCode == LOCATION_PERMISSION_REQUEST && grantResults.length > 0 && | |
| grantResults[0] == PackageManager.PERMISSION_DENIED) { | |
| text.setText("Location permission denied, please enable it in the settings"); | |
| // Denied permanently, prompt settings to user | |
| if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { | |
| Button btn = (Button) findViewById(R.id.btn); | |
| btn.setOnClickListener(v -> { | |
| var intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); | |
| intent.setData(Uri.parse("package:" + getPackageName())); | |
| startActivity(intent); | |
| }); | |
| } | |
| } else { | |
| // pass | |
| getCurrentLocation(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment