-1

I'm trying to buld an alarm with notificatoin and rington.

  1. How should it be done properly?

What I do: From my activity i call the BroadcastReceiver by using alarmmanager, than in BroadcastReceiver I make the notification with NotificationCompat.Builder and after that I call the service.

  1. How to cancel the rington by swipeing out or double clicking the notification ?

{

private Intent notificationIntent;

@Override
public void onReceive(Context context, Intent intent) {

    String title = intent.getStringExtra("myTitle");
    notificationIntent = new Intent();
    checkIntent(title, context);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.dumbbell)
            .setContentTitle("Tracker")
            .setContentText(title)
            .setAutoCancel(true);

    notificationManager.notify(1, builder.build());

    Intent ringtoneIntent = new Intent(context, AlarmService.class);
    context.startService(ringtoneIntent);
}

private void checkIntent(String title, Context context) {
    switch (title){
        case "Weight":
            notificationIntent = new Intent(context, WeightActivity.class);
            break;
        case "Measure":
            notificationIntent = new Intent(context, measurActivity.class);
            break;
        case "Pr":
            notificationIntent = new Intent(context, PrsActivity.class);
            break;
        case "Macros":
            notificationIntent = new Intent(context, DietActivity.class);
            break;
    }
}

public class AlarmService extends Service {

private MediaPlayer alarmSong;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    alarmSong = MediaPlayer.create(this, R.raw.willy_william_voodoo);
    alarmSong.start();
    System.out.println("RING RING");

    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    System.out.println("removed");
}

}

1 Answers1

0

As I understand, you'd like to stop your service AKA stop the ringtone. The user can dismiss all notification or if you set your notification to auto-cancel (which you have) it is also removed once the user selects it.

You can also call the cancel() for a specific notification ID on the NotificationManager. The cancelAll() method call removes all of the notifications you previously issued. Also you can implement new PendingIntent for service to stop itself.

Add to OnStartCommand()

notificationManager.cancel(NOTIFCATION_ID);
        stopSelf();

Then add new Intent

Intent stopSelf = new Intent(this, AlarmService.class);
    stopSelf.setAction(this.ACTION_STOP_SERVICE);
    PendingIntent pStopSelf = PendingIntent.getService(this, 0, stopSelf,PendingIntent.FLAG_CANCEL_CURRENT);
    notificationManager.notify(NOTIFCATION_ID, builder.build());

To stop the actual music playing call alarmSong.stop() onDestroy() in your service!

Hope it helps!

  • Don't forget to add your new intent to the BroadcastReciever! –  Nov 05 '17 at 19:33
  • why to cancel the notification? its cancel the notofication but the music continue – Tal Brodkin Nov 05 '17 at 20:07
  • @TalBrodkin I've edited my answer. Call .stop() on your MediaPlayer when the notification is cancelled –  Nov 05 '17 at 20:52
  • There is no onStop metod in the service. And again, how do catch the notification cancelled event ? – Tal Brodkin Nov 05 '17 at 20:57
  • Ok in maneged to catch the notification delete event (setDeleteIntent) from there its calls another BroadcastReceiver, now im just need to cancel the service/music, any ideas ? – Tal Brodkin Nov 05 '17 at 21:50
  • @TalBrodkin so the algorithm of your solution is - stop the service when cancel the notification (for more info how to do it see https://stackoverflow.com/questions/12872204/how-to-cancel-a-foreground-service-from-using-the-notification-swipe-dismiss-o/35721582) -then you just add MediaPlayer.stop() to your override onDestroy() method in your service and the music will be stopped when the service is stopped –  Nov 06 '17 at 09:19