我很惊讶在应用程序中发现我正在编程当我播放通知声音时,无论手机是否已设置为静音,它都会播放!
手机静音模式当然应该是最重要的功能还是我打算检查? 我快速查看了文档,但没有看到任何说这个的内容?我错过了什么吗?
这是我的通知代码:
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();
}
感谢您的反馈!
贝克斯
答案 0 :(得分:4)
根据测试结果,您似乎需要检查它:
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
// Play the sound
}
答案 1 :(得分:4)
不确定您选中的文档,但在“无声模式下的Nexus One”复选框中,它清楚地显示:
Silence all sounds except media & alarms.
在你的例子中,你正在播放闹钟(不是通知),所以它是正确的。
答案 2 :(得分: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);