How do I make sure that when a screen is running, my phone won’t go off or go into standby, and the application can’t exit before I tell it to with a button?
use the KeepScreenOn method from the tools extension
Taifun
how can i get this extension ?
bottom page,
how to make sure that when the application is running, all notifications from other applications do not appear on the screen? and how to make sure that when the application is running, the screen cannot be shared with other applications?
I asked Gemini for you, find the answer below…
You probably could write your own extension to implement this
Taifun
Ah, that’s a common challenge when you want to provide an immersive or distraction-free experience within your Android application. You essentially want to temporarily suppress notifications from other apps while yours is in the foreground.
Here’s a breakdown of the approaches you can take, ranging from simpler to more complex, along with their considerations:
- Using INTERRUPT_FILTER_NONE (Do Not Disturb - Programmatic Access):
This is the most direct and recommended way to achieve this, but it requires the user to grant your app Do Not Disturb access.
- How it works: Your app can programmatically request and then set the system’s interruption filter to INTERRUPT_FILTER_NONE, which effectively silences all notifications except those your app explicitly shows. When your app goes into the background or is closed, you should revert this setting.
- Steps:
-
Request ACCESS_NOTIFICATION_POLICY permission: Add this permission to your AndroidManifest.xml:
-
Check Do Not Disturb Access: Before attempting to change the interruption filter, check if your app has been granted the necessary access:
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (!notificationManager.isNotificationPolicyAccessGranted) {
// Request the user to grant Do Not Disturb access
Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS).apply {
startActivity(this)
}
// Inform the user why you need this permission
} else {
// Access granted, you can now change the interruption filter
setNoDisturbMode(true)
} -
Set the Interruption Filter: When your app is in the foreground (e.g., in onResume() of your main Activity or a specific immersive activity):
@RequiresApi(Build.VERSION_CODES.M)
fun setNoDisturbMode(enable: Boolean) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (notificationManager.isNotificationPolicyAccessGranted) {
val newInterruptionFilter =
if (enable) NotificationManager.INTERRUPT_FILTER_NONE
else NotificationManager.INTERRUPT_FILTER_ALL // Or a less restrictive filter
notificationManager.setInterruptionFilter(newInterruptionFilter)
} else {
// Handle the case where access is not granted (e.g., show a message)
}
} -
Revert the Filter: When your app goes into the background (e.g., in onPause() or onStop()) or is destroyed, make sure to revert the interruption filter to a less restrictive state (like INTERRUPT_FILTER_ALL or the user’s previous setting if you’ve stored it).
-
- Important Considerations:
- User Permission: This method requires the user to explicitly grant your app “Do Not Disturb access” in the system settings. You should clearly explain to the user why your app needs this permission and guide them through the process.
- API Level: The NotificationManager.setInterruptionFilter() method was introduced in Android 6.0 (API level 23). You’ll need to handle compatibility for older devices.
- User Experience: Be mindful of the user experience. Don’t silently block notifications without the user’s explicit consent. Clearly indicate when your app is suppressing notifications.
- Reverting: It’s crucial to revert the Do Not Disturb setting when your app is no longer in the foreground to avoid the user missing important notifications from other apps.
- Using Window Flags (Less Reliable for True Suppression):
You can try using window flags to make your activity more “immersive,” but this does not truly suppress other notifications. It primarily affects the visibility of system UI elements.
-
How it works: You can set flags on your activity’s window to hide the status bar and navigation bar. This can create a more focused experience, but notifications will still potentially appear (e.g., as heads-up notifications).
-
Example (in onCreate() or onResume() of your Activity):
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
} -
Limitations:
- Doesn’t block notifications: Heads-up notifications can still appear, and the user can still pull down the notification shade.
- Focus on UI: This method is primarily about creating an immersive UI, not about preventing other apps from sending notifications.
- Implementing Custom Notification Handling (More Complex and Limited Scope):
You could potentially try to listen for new notifications using NotificationListenerService, but this approach has significant limitations for your use case of preventing other apps’ notifications from appearing:
- NotificationListenerService: This service allows your app to see notifications posted by other apps. However, it cannot directly prevent those notifications from being displayed by the system. You can only observe and react to them (e.g., log them, display your own custom UI related to them, but not stop the original from appearing).
- Security and Permissions: Using NotificationListenerService requires the user to explicitly grant your app “Notification access,” which is a sensitive permission. Users are often wary of granting this level of access.
- Not for Suppression: This service is designed for notification management apps, accessibility services, and similar use cases, not for globally suppressing other apps’ notifications while your app is running.
Therefore, the most effective and appropriate way to temporarily prevent other applications’ notifications from appearing while your Android application is running is by using the NotificationManager.setInterruptionFilter(NotificationManager.INTERRUPT_FILTER_NONE) method, but remember that this requires the user to grant your app Do Not Disturb access. You must handle the permission request gracefully and clearly communicate the purpose of this access to the user. Always remember to revert the filter when your app is no longer in the foreground.