我正在 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/