2011-05-25 28 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

相關問題