2017-04-11 167 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這爲我工作。 –