2014-12-06 241 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