侦听什么以检测Android中的请勿打扰模式变化?

yas*_*asd 5 android

我希望我的应用程序在手机设置为“请勿打扰”模式(仅警报,仅优先级或完全静音)时显示通知。通过android.media.RINGER_MODE_CHANGED在快速设置中检查此模式并在已选择的选项卡中选择该模式时,可以很好地工作。但是当选择另一个标签时,它不会再次被触发。因此我的应用程序具有错误的模式信息并显示错误的通知。

我再次尝试,android.app.action.INTERRUPTION_FILTER_CHANGED但在上述情况下根本没有触发。

我想立即得到通知,所以我不想使用轮询观察器。就我而言,我希望由此而消耗大量电池。

有一个类似的问题,但没有侦听器解决方案:

Android:检测“请勿打扰”状态?

希望在此期间我想再次提出一个解决方案:有人有什么好主意或提示要听什么吗?

更新:

这是AndroidManifest.xml中的广播接收器定义...要明确这一点:接收器在振铃模式更改时被调用,还检测到飞行模式和摘机,但是并非在所有振铃模式更改时都被调用。通过按快速设置或使用音量键在免打扰模式更改之间切换:

    <receiver android:name="UpdateStatusNotification" android:process=":remote">
        <intent-filter>
            <action android:name="android.app.action.INTERRUPTION_FILTER_CHANGED"/>
            <action android:name="android.intent.action.AIRPLANE_MODE"/>
            <action android:name="android.intent.action.PHONE_STATE"/>
            <action android:name="android.media.RINGER_MODE_CHANGED"/>
        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)

更新2:

请阅读我对已接受答案的最后评论。

Nav*_* pk 5

广播接收器动作是:

NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED

IntentFilter intent = new IntentFilter();
intent.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
Run Code Online (Sandbox Code Playgroud)

下面的代码应该在接收器中。

if(intetn.getAction().equals(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED) {
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    if(mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_ALARMS) {
        //do your stuff
    } else if (mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_NONE) {
        //....
    } else if (mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_ALL) {
        //....
    } else if (mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_PRIORITY) {
        //....
    } else if (mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_UNKNOWN) {
        //....
    }
}
Run Code Online (Sandbox Code Playgroud)