Skip to content

Instantly share code, notes, and snippets.

@iyotetsuya
Created March 17, 2023 06:54
Show Gist options
  • Save iyotetsuya/08fcd889ebbf18dc95fef9110b805dfe to your computer and use it in GitHub Desktop.
Save iyotetsuya/08fcd889ebbf18dc95fef9110b805dfe to your computer and use it in GitHub Desktop.
Android 14 API Sample: Activity.ScreenCaptureCallback
package com.iyotetsuya.android14playgrounds
import android.app.Activity
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.iyotetsuya.android14playgrounds.ui.theme.Android14PlaygroundsTheme
@RequiresApi(34)
class DetectScreenshotsActivity : ComponentActivity(), Activity.ScreenCaptureCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Android14PlaygroundsTheme {
Box(Modifier.fillMaxSize()) {
Text(
modifier = Modifier
.padding(4.dp)
.align(Alignment.Center),
text = "Detect Screenshots Activity"
)
}
}
}
}
override fun onStart() {
super.onStart()
registerScreenCaptureCallback(mainExecutor, this)
}
override fun onStop() {
super.onStop()
unregisterScreenCaptureCallback(this)
}
override fun onScreenCaptured() {
Toast.makeText(
this, "Screen Captured.", Toast.LENGTH_SHORT
).show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment