Skip to content

Commit ac20514

Browse files
committed
Moving location permissions snippets to repo
1 parent 273d399 commit ac20514

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.example.snippets.location;
2+
3+
import android.Manifest;
4+
import android.os.Build;
5+
import android.os.Bundle;
6+
import androidx.activity.ComponentActivity;
7+
import androidx.activity.result.ActivityResultLauncher;
8+
import androidx.activity.result.contract.ActivityResultContracts;
9+
import java.util.Map;
10+
11+
public class LocationPermissionsActivity extends ComponentActivity {
12+
13+
// [START misc_location_requestpermissions_java]
14+
private void requestPermissions() {
15+
16+
ActivityResultLauncher<String[]> locationPermissionRequest =
17+
registerForActivityResult(new ActivityResultContracts
18+
.RequestMultiplePermissions(), result -> {
19+
20+
Boolean fineLocationGranted = null;
21+
Boolean coarseLocationGranted = null;
22+
23+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
24+
fineLocationGranted = result.getOrDefault(
25+
Manifest.permission.ACCESS_FINE_LOCATION, false);
26+
coarseLocationGranted = result.getOrDefault(
27+
Manifest.permission.ACCESS_COARSE_LOCATION,false);
28+
}
29+
30+
if (fineLocationGranted != null && fineLocationGranted) {
31+
// Precise location access granted.
32+
} else if (coarseLocationGranted != null && coarseLocationGranted) {
33+
// Only approximate location access granted.
34+
} else {
35+
// No location access granted.
36+
}
37+
}
38+
);
39+
40+
// ...
41+
42+
// Before you perform the actual permission request, check whether your app
43+
// already has the permissions, and whether your app needs to show a permission
44+
// rationale dialog. For more details, see Request permissions.
45+
locationPermissionRequest.launch(new String[] {
46+
Manifest.permission.ACCESS_FINE_LOCATION,
47+
Manifest.permission.ACCESS_COARSE_LOCATION
48+
});
49+
}
50+
// [END misc_location_requestpermissions_java]
51+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.example.snippets.location
2+
3+
import android.os.Build
4+
import androidx.annotation.RequiresApi
5+
import android.Manifest
6+
import androidx.activity.ComponentActivity
7+
import androidx.activity.result.contract.ActivityResultContracts
8+
9+
// Assuming this function is inside a ComponentActivity or a subclass of it
10+
class LocationActivity : ComponentActivity() {
11+
12+
// [START misc_location_requestpermissions_kotlin]
13+
@RequiresApi(Build.VERSION_CODES.N)
14+
fun requestPermissions() {
15+
val locationPermissionRequest = registerForActivityResult(
16+
ActivityResultContracts.RequestMultiplePermissions()
17+
) { permissions ->
18+
when {
19+
permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> {
20+
// Precise location access granted.
21+
}
22+
permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false) -> {
23+
// Only approximate location access granted.
24+
}
25+
else -> {
26+
// No location access granted.
27+
}
28+
}
29+
}
30+
31+
// Before you perform the actual permission request, check whether your app
32+
// already has the permissions, and whether your app needs to show a permission
33+
// rationale dialog. For more details, see Request permissions:
34+
// https://developer.android.com/training/permissions/requesting#request-permission
35+
locationPermissionRequest.launch(
36+
arrayOf(
37+
Manifest.permission.ACCESS_FINE_LOCATION,
38+
Manifest.permission.ACCESS_COARSE_LOCATION
39+
)
40+
)
41+
}
42+
// [END misc_location_requestpermissions_kotlin]
43+
}

0 commit comments

Comments
 (0)