2017-04-11 125 views
-1

我已实施的有声本地通知。它工作正常,但是当我清除本地通知时,自定义声音应该停止。停止本地通知声音的Android

Uri uri = null; 
String[] split = content.split("-"); 
Intent intents = new Intent(context, MainActivity.class); 
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intents, 0); 
//getting audio from raw folder 
uri = Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.beep); 

//plays audio infinite times until user clicks or clear notification 
try 
{ 
    MediaPlayer player = MediaPlayer.create(context, uri); 
    player.setLooping(true); 
    player.start(); 

    Notification noti = new Notification.Builder(context) 
     .setContentTitle(split[0]) 
     .setContentText(device_name + split[1]).setSmallIcon(R.drawable.hi_notification) 
     .setContentIntent(pIntent) 
     .setDefaults(Notification.DEFAULT_ALL) 
     .setPriority(Notification.PRIORITY_HIGH) 
     .build(); 

    noti.sound = uri; 
    noti.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE; 

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE); 
    // hide the notification after its selected 
    noti.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE; 

    notificationManager.notify(0, noti); 
} 
catch (IllegalStateException e) 
{ 
    e.printStackTrace(); 
}   
+0

我觉得只是添加 .setAutoCancel(真)后,会取消的通知,并停止声音太 –

+0

感谢您的答复。我试过这不止步。 –

回答

0

设置通知的声音是坚持。

1.Notification.DEFAULT_SOUND:用于播放声音。

2.Notification.FLAG_INSISTENT:这个标志让你的声音不断响起,直到你的通知,即做一些操作,要么你拖动滑块,或者您点击了吧。

3.Notification.FLAG_AUTO_CANCEL:这个标志是用来自动取消通知你已经看到了

builder.setSmallIcon(R.mipmap.app_icon).setContentTitle(title) 
      .setContentText(msg) 
      .setColor(getResources().getColor(R.color.white)) 
      .addAction(R.drawable.alarm_off, getString(R.string.dismiss), pendingIntent) 
      .setOngoing(true) 
      .setSound(Uri.parse(path)); 

    Notification notification = builder.build(); 
    notification.flags |= Notification.FLAG_INSISTENT; 


    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(WAKE_UP_ALARM_ID, notification) 
+0

非常感谢您在我清除通知时停止通话,但点击它不会停止。我该如何做到这一点。 –

+0

试试这个代码,并设置标志notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_NO_CLEAR; – pradeep

+0

谢谢你,但它不力work.Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL这为我工作。 –