Stopping Foreground Service on Notification Click

2019-07-12 18:42发布

问题:

I'm learning about android services. I have a button in my main activity that, when clicked, uses a mediaplayer to start playing a sound file, and a notification is shown. I want the music service to be stopped and the notification removed when I click on the notification. I've been bashing my head against the wall for a few hours now as I'm unable to figure out what I'm doing wrong. Here is my service class:

public class MusicService extends Service {
public MusicService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    MediaPlayer player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
    player.setLooping(true);
    player.start();

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle("Hello")
            .setTicker("Hello 2")
            .setContentText("Hello 3")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentIntent(pendingIntent)
            .setOngoing(true)
            .build();

    startForeground(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);


    return START_STICKY;
}

public interface NOTIFICATION_ID {
    public static int FOREGROUND_SERVICE = 101;
}

}

回答1:

What's the problem to stop foreground service and open activity on click notification by passing pending intent to broadcast:

Create broadcast receiver

public class MusicNotificationBroadcastReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
      // Start Activity
      Intent activityIntent = new Intent(context, MainActivity.class);
      activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(activityIntent);
      // Start Services
      startService(new Intent(this, MusicService.class).setAction("STOP_ACTION"));
   }
}

Make notification:

Intent intent = new Intent(context, MusicNotificationBroadcastReceiver.class);
PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

Notification notification = new NotificationCompat.Builder(this)
        .setContentTitle("Hello")
        .setTicker("Hello 2")
        .setContentText("Hello 3")
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentIntent(contentIntent)
        .setOngoing(true)
        .build();

So on onStartCommand handle this:

if(intent.getAction() != null && intent.getAction().equals("STOP_ACTION")) {
     stopForeground(true);
}