我试图抓住所有通知并根据通知的联系播放特定声音。 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个声音: 我的自定义声音 2.原始声音
有没有办法覆盖原始声音?我只能听到自定义的一个。
我有trid:
sbn.getNotification().sound=SinglesoundUri;
但它没有播放声音,实际上是sbn.getNotification().sound==null
缓存StatusBarNotification时。
答案 0 :(得分:0)
您还必须通过设置通知默认值来覆盖默认声音 使用以下代码:
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));