Good day. There is an application that scans all received SMS messages. If the application is turned off and at this moment a message has arrived, addressed to the application, then I display Notification. Clicking on Notification opens the main activation and starts playing Ringtone (which is launched from Notification builder`a).

Question: How to stop the melody on the main screen? When I open the main screen, I display an AlertDialog with the "OK" button. By clicking on this button, I want to stop the music, but how can I get a copy of it to stop ??

Here is the code for the notification call in BroadcastReceiver:

void myNotification(final Context context){ notificationAlarm = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } Intent notificationIntent = new Intent(context, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); Resources res = context.getResources(); Notification.Builder builder = new Notification.Builder(context); builder.setContentIntent(contentIntent) .setSmallIcon(R.drawable.notify_icon) .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notify_icon)) .setTicker("Пришло новое сообщение!") .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setSound(ringURI) .setContentTitle("Сообщение") .setContentText("Получено новое сообщение"); Notification notification = builder.build(); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFY_ID, notification); } }); notificationAlarm.start(); } 
  • 1. Have you tried to just cancel the notification? 2. You can set the flag builder.flags | = Notification.FLAG_AUTO_CANCEL; - Andrew Grow
  • @AndrewGrow I seem to have the auto-cancel flag - .setAutoCancel (true) - ivanovd422
  • And the sound does not turn off when you cancel? - Andrew Grow
  • @AndrewGrow not, that's just the point. As you can see, I launch the notification in a separate thread and at the same time wait 2000 miles for seconds. It seems to me that the problem may be exactly this. This is due to the fact that at the time of the arrival of the SMS message plays a system notification. If at this moment to launch my melody, it will not play. Therefore, I wait 2 seconds. - ivanovd422
  • Can the flow stop? notificationAlarm.interrupt () - Andrew Grow

0