Skip to content

Commit 82573fd

Browse files
authored
Moving location permissions snippets to repo (#413)
* Moving location permissions snippets to repo * Apply Spotless * rationalize class names * Fixing region tags --------- Co-authored-by: jakeroseman <[email protected]>
1 parent 273d399 commit 82573fd

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.snippets.location
18+
19+
import android.Manifest
20+
import android.os.Build
21+
import androidx.activity.ComponentActivity
22+
import androidx.activity.result.contract.ActivityResultContracts
23+
import androidx.annotation.RequiresApi
24+
25+
// Assuming this function is inside a ComponentActivity or a subclass of it
26+
class LocationPermissionsActivity : ComponentActivity() {
27+
28+
// [START android_location_requestpermissions_kotlin]
29+
@RequiresApi(Build.VERSION_CODES.N)
30+
fun requestPermissions() {
31+
val locationPermissionRequest = registerForActivityResult(
32+
ActivityResultContracts.RequestMultiplePermissions()
33+
) { permissions ->
34+
when {
35+
permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> {
36+
// Precise location access granted.
37+
}
38+
permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false) -> {
39+
// Only approximate location access granted.
40+
}
41+
else -> {
42+
// No location access granted.
43+
}
44+
}
45+
}
46+
47+
// Before you perform the actual permission request, check whether your app
48+
// already has the permissions, and whether your app needs to show a permission
49+
// rationale dialog. For more details, see Request permissions:
50+
// https://developer.android.com/training/permissions/requesting#request-permission
51+
locationPermissionRequest.launch(
52+
arrayOf(
53+
Manifest.permission.ACCESS_FINE_LOCATION,
54+
Manifest.permission.ACCESS_COARSE_LOCATION
55+
)
56+
)
57+
}
58+
// [END android_location_requestpermissions_kotlin]
59+
}
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 LocationPermissionsActivityJava extends ComponentActivity {
12+
13+
// [START android_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 android_location_requestpermissions_java]
51+
}

0 commit comments

Comments
 (0)