Android, Phone playing notification sounds even when on silent? Should I be checking for silent mode?
I was very surprised to find in the app I am currently programming that when I make it play a notification sound, it plays regardless of whether the phone has been set to silent or not!
Surely the phones silent mode should be an overriding feature or am I meant to be checking? I had a quick look at the documentation but did not see anything saying this? Am I missing something?
This is my notification code:
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();
}
Thanks for any feedback here!
Bex
Not sure about the documentation you checked, but on Nexus One below Silent mode checkbox it clearly says:
Silence all sounds except media & alarms.
In your example you are playing alarm (not notification), so it is correct.
Based on the testing results it seems that you're the one that needs to check it:
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
// Play the sound
}
1) set 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
start method
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);
stop method
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);
精彩评论