2011-05-25 29 views
8

我很惊讶地发现,在我正在编程的应用程序中,当我使它播放通知声音时,无论手机是否已设置为无声,它都会播放!Android,电话播放通知即使在静音时也会响起?我应该检查静音模式吗?

手机无声模式肯定应该是一个压倒一切的功能,或者我的意思是检查? 我有一个快速浏览文档,但没有看到任何说这个?我错过了什么吗?

这是我的通知码:

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

感谢这里的任何反馈!

贝克斯

+1

我有同样的问题,但是我正在播放普通的声音文件(它不是闹钟)。有任何想法吗 ? – Leeeeeeelo 2013-05-17 06:42:47

回答

4

不知道你选中的文件,但下面静音模式的Nexus One复选框上面清清楚楚地写着:

Silence all sounds except media & alarms. 

在你的榜样,你都打报警(不通知),所以它是正确的。

+0

哦,我没有意识到这一点!教我复制代码!也没有意识到警报仍然会消失! :) – Bex 2011-05-25 10:36:19

+0

好的,对于“外部”人来说,通过短视来发现您的问题总是更容易。问候。 – Zelimir 2011-05-25 10:47:47

4

基于测试结果看来,你需要检查它的人:

if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) { 
    // Play the sound 
} 
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); 
+0

我觉得,它不好接近。因为我们不知道什么时候再次设置音频管理器的currentVolume。 – 2018-02-05 11:17:04

相关问题