android - 更改其他应用程序的通知声音

标签 android audio android-notifications

我正在 try catch 所有通知并根据通知的联系人播放特定的声音。
myNotificationListener 等待传入的 StatusBarNotification 并决定播放什么声音,然后将其发送到 myCustomNotification 。它发送通知的位置。
目前我的代码是这样的:

 public class myNotificationListener extends NotificationListenerService {
    Context context;
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();

    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        if (isPreferenceOn()) {
            String pack = sbn.getPackageName();

//         take Notification details
            Bundle extras = sbn.getNotification().extras;
            String title = extras.getString("android.title");
            String text = extras.getString("android.text");
            String app = sbn.getPackageName();
            int id = sbn.getId();

            Log.i("Text", "" + text);


                //find sounds by trigers
                Uri DoublesoundUri = null;
                if (Double != null)
                    DoublesoundUri = SoundResolver.getSound(text );
                Uri SinglesoundUri = null;
                if (Single != null)
                    SinglesoundUri = SoundResolver.getSound(text );

                //decide witch sound to play
                if (DoublesoundUri != null) {

                    not.sound = DoublesoundUri;

                    //new myCustomNotification(DoublesoundUri, Double, context);
                } else if (SinglesoundUri != null) {

                    not.sound = SinglesoundUri;

                    //   new myCustomNotification(SinglesoundUri, Single, context);
                }



        }
    }

还有我的自定义通知类
public class myCustomNotification {


    myCustomNotification(Uri soundUri,String text, Context context) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSound(soundUri)
                         .setSmallIcon(R.drawable.ic_notifications_white_24dp)
                          .setContentTitle(text)
                            .setColor(context.getResources().getColor(R.color.colorAccent));

        // Sets an ID for the notification
        int mNotificationId = 2580;

// Gets an instance of the NotificationManager service
        NotificationManager mNotifyMgr =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
        mNotifyMgr.notify(mNotificationId, mBuilder.build());
    }


}

问题是我得到 2 种声音而不是 1 种:
1.我的自定义声音
2.原声

有什么办法可以覆盖原始声音?我只会听到自定义的。

我有三毛:
            sbn.getNotification().sound=SinglesoundUri;

但它不播放声音,实际上sbn.getNotification().sound==null当 StatusBarNotification 被缓存时。

最佳答案

您还必须通过设置通知默认值来覆盖默认声音
使用下面的代码:

String sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd) ;

myCustomNotification(Uri soundUri,String text, Context context) {
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)//it replaces your default voice
                    .setSound(soundUri)
                     .setSmallIcon(R.drawable.ic_notifications_white_24dp)
                      .setContentTitle(text)
                        .setColor(context.getResources().getColor(R.color.colorAccent));

关于android - 更改其他应用程序的通知声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39932723/

相关文章:

具有前台服务的 Android worker 不显示通知

android - 如何将访问 token 附加到 Google 表格 V4 请求?

android - 将 .java 直接转换为 .smali。可能的?

java - 单击 ViewPager 中的特定页面后如何跳转到另一个 Activity ?

c - 无论质量如何,都以最佳压缩方式用 C++ 录制音频

java - android 通知 - RemoteServiceException

java - 房间 : LiveData One To Many Relation

html - 如何在事件监听器上加载下一首轨道?

android - 防止媒体播放器同时播放多个声音

android - RemoteViews setImageViewBitmap() 不工作