Skip to content

Instantly share code, notes, and snippets.

@fredchung
fredchung / ShareUri.kt
Last active September 7, 2020 06:03
Sharing content Uri's with other apps.
val shareIntent = Intent(Intent.ACTION_VIEW).apply {
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
data = // Content Uri to be shared with others
}
@fredchung
fredchung / RequestLocationPermissions.kt
Last active September 7, 2020 06:06
Do not request foreground and background location permissions together (ActivityCompat or Framework APIs)
requestPermissions(
arrayOf(
// Do not request foreground and background location permissions together,
// as they will be ignored along with any other permissions in the request.
// Request location permissions incrementally instead.
android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.ACCESS_BACKGROUND_LOCATION,
...)
)
@fredchung
fredchung / RequestLocationPermissions.kt
Created September 7, 2020 06:07
Do not request foreground and background location permissions together (Activity library).
// Using Activity library.
val requestPermissionsLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
map: MutableMap<String, Boolean> ->
...
}
...
requestPermissionsLauncher.launch(
arrayOf(
@fredchung
fredchung / AndroidManifest.xml
Created September 7, 2020 06:09
Foreground service type declarations.
android:foregroundServiceType = "microphone | location | camera"
@fredchung
fredchung / AndroidManifest.xml
Created September 7, 2020 06:10
Foreground service type declaration for WorkManager long running workers.
<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="location|microphone"
tools:node="merge" />
@fredchung
fredchung / Worker.kt
Created September 7, 2020 06:12
Setting foreground service types in WorkManager long-running workers.
setForeground(
ForegroundInfo(NOTIF_ID,
notification.build(),
// Foreground service types.
ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION or ...)
)