Android Notification Action is not fired (PendingIntent)

前端 未结 9 1642
盖世英雄少女心
盖世英雄少女心 2020-12-30 02:39

I am trying to add an Notification action item in my app which is a music player. When a stream is started a notification should be triggered and an stop button for the stre

9条回答
  •  情书的邮戳
    2020-12-30 03:21

    More than using broadcast receiver, you should use a service and declare a new action in your service this way:

    public final String ACTION_STOP = "yourpackagename.ACTION_STOP";
    

    And then create your intents like this:

    Intent stopIntent = new Intent(this, YourService.class).setAction(YourService.ACTION_STOP);
    PendingIntent stopPendingIntent = PendingIntent.getService(this, 0, stopIntent, 0);
    

    Of course, stop playback in your service's function startCommand, if the intent's action equals ACTION_STOP.

    This should do the trick ;)

提交回复
热议问题