【问题标题】:Android, Phone playing notification sounds even when on silent? Should I be checking for silent mode?Android,即使在静音状态下,手机播放通知也会响起?我应该检查静音模式吗?
【发布时间】:2017-09-20 18:41:25
【问题描述】:

我很惊讶地发现,在我正在编程的应用程序中,当我让它播放通知声音时,无论手机是否设置为静音,它都会播放!

当然,手机静音模式应该是最重要的功能,还是我要检查一下? 我快速浏览了文档,但没有看到任何内容?我错过了什么吗?

这是我的通知代码:

    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(300);

        Uri alert = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        MediaPlayer mMediaPlayer = new MediaPlayer();

        mMediaPlayer.setDataSource(this, alert);

        final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.setLooping(false);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        }

感谢您在这里的任何反馈!

贝克斯

【问题讨论】:

  • 我有同样的问题,但是我正在播放一个普通的声音文件(它不是警报)。有什么想法吗?

标签: android


【解决方案1】:

不确定您检查的文档,但在静音模式下的 Nexus One 复选框上清楚地显示:

Silence all sounds except media & alarms.

在您的示例中,您正在播放警报(而不是通知),所以它是正确的。

【讨论】:

  • 哦,我没有意识到这一点!教我抄码!也没有意识到警报仍然会响起! :)
  • 好的,“外人”总是更容易通过短视发现您的问题。问候。
【解决方案2】:

根据测试结果,您似乎是需要检查的人:

if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
    // Play the sound
}

【讨论】:

    【解决方案3】:

    1) 设置 StandupReciver.java

    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.media.AudioManager;
    import android.provider.Settings;
    import android.support.v4.app.NotificationCompat;
    import android.widget.Toast;
    
    
    public class StandupReciver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"inside Recevier",Toast.LENGTH_SHORT).show();
            NotificationManager myManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder mynoti=new NotificationCompat.Builder(context);
        // FOR SILENT MODE
        AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    
        // For Normal mode
        int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
        int maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float percent = 2.0f;
        int seventyVolume = (int) (maxVolume*percent);
        audio.setStreamVolume(AudioManager.STREAM_MUSIC, seventyVolume, 0);
        audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        audio.setStreamVolume(AudioManager.STREAM_MUSIC, audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
    
    
        mynoti.setContentTitle("Stand up Notification");
        mynoti.setContentText("you need to stand up");
        mynoti.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
        mynoti.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
        mynoti.setSmallIcon(android.R.drawable.ic_btn_speak_now);
    
    
    
        Intent il=new Intent(context,MainActivity.class);
        PendingIntent pd= PendingIntent.getActivity(context,0,il,0);
        mynoti.setContentIntent(pd);
        mynoti.setAutoCancel(true);
        myManager.notify(1,mynoti.build());
    }}
    

    2) AndroidManifest.xml

    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
    <receiver
            android:name=".StandupReciver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="com.xyz.myown.recevier.Message"/>
                <category  android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
    

    3)MainActivity.java

    启动方法

    Toast.makeText(getApplicationContext(), "Start", Toast.LENGTH_SHORT).show();
                    Intent i = new Intent();
                    i.setAction("com.xyz.myown.recevier.Message");
                    i.addCategory("android.intent.category.DEFAULT");
                    PendingIntent pd = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10, pd);
    

    停止方法

      Toast.makeText(getApplicationContext(),"Stop",Toast.LENGTH_SHORT).show();
                Intent i=new Intent();
                i.setAction("com.xyz.myown.recevier.Message");
                i.addCategory("android.intent.category.DEFAULT");
                PendingIntent pd= PendingIntent.getBroadcast(getApplicationContext(),0,i,0);
                alarmManager.cancel(pd);
    

    【讨论】:

    • 我认为,这不是很好的方法。由于我们不知道何时再次设置音频管理器的 currentVolume。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    相关资源
    最近更新 更多