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(); }