安卓手机在静音模式下也会播放通知声音?我是否应该检查静音模式?

8

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

手机的静音模式肯定应该是一个优先特性,或者说我需要进行检查吗?我快速查看了文档,但没有看到任何相关说明?我有什么遗漏吗?

这是我的通知代码:

    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();
        }

感谢您在此处提供任何反馈!Bex

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

4

根据测试结果,看起来需要检查的是您自己:

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

4

我不确定您查阅的文档,但在 Nexus One 上,在“静音模式”复选框下方明确写着:

Silence all sounds except media & alarms.

在您的示例中,您正在播放警报声(而不是通知),因此是正确的。

1
哦,我没意识到这个!这就是抄袭代码的代价!但我也没想到闹钟还会响起来! :) - Bex
好的,对于外部人员来说,通过简短的查看发现您的问题总是更容易。问候。 - Zelimir

0

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。 - Kimmi Dhingra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接