1

我正在开发一个简单的应用程序,它可以STREAM_NOTIFICATION在特定时间静音并在特定运行时间后取消静音。

AlarmManager用来触发动作并执行setStreamMute(STREAM_NOTIFICATION, true/false)。Alarm set的核心代码如下:onReceivedBroadcastReceiver

// mute task
Calendar muteCal = getTaskCalendar(i, curTask.getHour(), curTask.getMin(), 0);
PendingIntent mutePendingIntent = getBoardCastPendingIntent(ctxt, "tag", "Mute", muteRequestCodeBase);

// unmute task
Calendar unMuteCal = getTaskCalendar(i, curTask.getHour(), curTask.getMin(), curTask.getPeriod());
PendingIntent unmutePendingIntent = getBoardCastPendingIntent(ctxt, "tag", "UnMute", unmuteRequestCodeBase);

alarm.setRepeating(AlarmManager.RTC_WAKEUP, muteCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, mutePendingIntent);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, unMuteCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, unmutePendingIntent);

以下是静音代码和 onReceive 函数所做的事情

public void onReceive(Context ctxt, Intent intent) {
     MuterHelper.setToMute(ctxt);
}
public class MuterHelper {
    public static void setToMute(Context context) {
            AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            am.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
    }

静音时间到了,noReceive setToMute,新短信到了也不会有声音提示,一切正常。

但是,在特定时间后,STREAM_NOTIFICATION将自动取消静音,根据 API 文档(静音命令可防止客户端进程死亡:如果对流具有活动静音请求的进程死亡,则该流将自动取消静音),并且我查了系统日志,有一个日志说

"00:53:33.250 W/AudioService(  312): Volume service client died for stream: 5" 

好的,这是我的问题。我想知道如何使服务客户端保持活动状态并保持NOTIFICATION取消静音,直到另一个取消静音任务setStreamMute(false)

感谢您的所有帮助!

4

0 回答 0