2014-12-06 238 views
1

我讀過一些關於這個問題的其他帖子,我認爲我的代碼應該鳴響 報警,但事實並非如此。它確實振動,但沒有聲音。有關如何獲得 傳達聲音的任何建議?Android通知,振動,但沒有聲音

程序的另一部分是能夠播放鈴聲,所以問題似乎是 具體此例程。

這是一個擴展服務

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    if (sound == null) { Log.i("RECEIVER", "SOUND IS NULL"); } 

    NotificationManager myNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    Intent intentMain = new Intent(this.getApplicationContext(), MainActivity.class); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intentMain, 0); 

    long[] pattern = {500,500,500,500,500,500,500,500,500}; 

    Notification myNote = new Notification.Builder(this) 
      .setContentTitle("NotificationDemo") 
      .setContentText("NotificatinDemo") 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setSound(sound) 
      .setVibrate(pattern) 
      .setContentIntent(pIntent) 
      .build(); 

    myNM.notify(1, myNote); 
    return super.onStartCommand(intent, flags, startId); 
} 
+0

謝謝,夥計們。這些都沒有奏效。我仍然在振動。我是否需要在應用清單中設置任何內容? – Marlonez 2014-12-07 05:20:52

+0

只是一個猜測:嘗試在發佈通知之前調用'super.onStartCommand()'。也許你需要先初始化服務。 – Jerry101 2015-09-22 04:01:28

回答

0

類首先檢查你的設備的通知聲音設置。

那就試試這個

NotificationCompat.Builder mBuilder = 
     new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_notification) 
       .setContentTitle(getString(R.string.app_name)) 
       .setContentText(someText) 
       .setDefaults(Notification.DEFAULT_SOUND) 
       .setAutoCancel(true); 
+0

嗨Vijay。感謝您的建議。我查看了手機的默認通知聲音,並將其設置爲非「無」值。即使您提出的代碼更改,我仍然沒有得到任何聲音,只是振動。 – Marlonez 2014-12-07 05:24:53

0

試試這個代碼

Notification myNote = new Notification.Builder(this) 
      .setContentTitle("NotificationDemo") 
      .setContentText("NotificatinDemo") 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setSound(Notification.DEFAULT_SOUND) 
      .setVibrate(pattern) 
      .setContentIntent(pIntent) 
      .build(); 
4

試試這個:

Notification.Builder mBuilder = new Notification.Builder(this) 
       .setContentTitle("NotificationDemo") 
       .setContentText("NotificatinDemo") 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setVibrate(pattern) 
       .setContentIntent(pIntent); 

Notification myNote = mBuilder.build(); 

if(Build.VERSION.SDK_INT >= 21) { 
    myNote.sound = sound; 
    myNote.category = Notification.CATEGORY_ALARM; 

    AudioAttributes.Builder attrs = new AudioAttributes.Builder(); 
    attrs.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION); 
    attrs.setUsage(AudioAttributes.USAGE_ALARM); 
    myNote.audioAttributes = attrs.build(); 
} else { 
    mBuilder.setSound(sound, AudioManager.STREAM_ALARM); 
    myNote = mBuilder.build(); 
} 

myNM.notify(1, myNote); 

同時檢查這些:Lollipop notification featureAudioAttributes class used in new notification systemusage of Audio Attributes