我想在我的Android应用程序处于前台时隐藏其他应用程序的所有通知.这可能吗?怎么样?

mil*_*n m 12 android push-notification firebase

我正在创建一个教育应用程序,学校的学生可以出现5分钟的快速考试.大多数时候他们使用父母的手机.

现在我想要的是,当学生参加考试时,任何其他应用程序(如WhatsApp,FB或Gmail)都不会发出任何通知.

这可能吗?怎么样?

fej*_*ejd 9

一种可能的解决方案是将手机设置为免打扰(Do Not Disturb)模式.我没有测试过这个,但根据你应该能够做到的文档:

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int currentFilter = notificationManager.getCurrentInterruptionFilter();
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
// Run the exam...
// Restore the filter
notificationManager.setInterruptionFilter(currentFilter);
Run Code Online (Sandbox Code Playgroud)

这需要特殊的DND访问权限,它需要Android M:

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
Run Code Online (Sandbox Code Playgroud)

用户需要通过"设置"接受该权限.有关更多背景信息以及如何启动导致此设置的活动,请参阅此问题.

第二种选择是使用在Android L中引入的屏幕固定.