通知声音在安卓API 10上无法工作

4

我使用这个函数来显示状态 notification。所有的东西都是正确的,但是没有声音在 notification 上播放。

public void notifiction_main(String ticker,String title,String text,int _icon){

        String ns = mContext.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(ns);
        int icon = R.id.icon;
        CharSequence tickerText = ticker; // ticker-text
        long when = System.currentTimeMillis();
        CharSequence contentTitle = title;
        CharSequence contentText = text;
        Intent notificationIntent = new Intent(mContext, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.setLatestEventInfo(mContext, contentTitle, contentText,contentIntent);
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        mNotificationManager.notify(1, notification);


    }

我设置了震动,但震动也没有起作用 :(

你在哪个API上工作? - Pankaj
使用 notification.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);); - Pankaj
通知没有设置声音方法。我在API 10中工作。 - bnnoor
使用 NotificationCompat.Builder - Pankaj
如果minSdk版本高达11,哪些代码可以工作? - bnnoor
2个回答

3
您可以使用NotificationCompat.Builder
NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
    .setContentTitle("Title")
    .setContentText("your_message")
    .setContentIntent(pendingIntent)
    .setAutoCancel(true)
    .setPriority(NotificationCompat.PRIORITY_HIGH);

notification.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
long[] pattern = {500,500,500,500};
notification.setVibrate(pattern);

2

在API level 20及以上的版本中,Notification类已经过时,建议您使用NotificationCompat.Builder。

以下是一段代码,希望对您有所帮助:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        //icon appears in device notification bar and right hand corner of notification
        builder.setSmallIcon(R.drawable.notificationg);
        Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),
                R.drawable.ic_launcher);
        builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(icon));
        Intent i = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), i, 0);

        builder.addAction(R.drawable.ic_launcher,"OK",pIntent);
        // This intent is fired when notification is clicked
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // Set the intent that will fire when the user taps the notification.
        builder.setContentIntent(pendingIntent);

        // Large icon appears on the left of the notification
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));

        // Content title, which appears in large type at the top of the notification
        builder.setContentTitle(notificationTitle);

        // Content text, which appears in smaller text below the title
        builder.setContentText(notificationMessage);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(alarmSound);
        // The subtext, which appears under the text on newer devices.
        // This will show-up in the devices with Android 4.2 and above only
        builder.setSubText("Tap to go to link in notifications.");

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar
        notificationManager.notify(NOTIFICATION_COUNT, builder.build());

通知铃声将会和以下代码一起使用:

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); builder.setSound(alarmSound);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接