Android 通知中的音乐播放器控件

Android 通知中的音乐播放器控件,android,android-mediaplayer,android-notifications,android-broadcast,Android,Android Mediaplayer,Android Notifications,Android Broadcast,如何在android中设置播放/暂停、下一步和上一步按钮的通知 我是Android的新手&也是stackoverflow的新手。所以请容忍我 我在歌曲开始播放时设置通知,如下所示: ` ` 我创建了如下广播接收器: ` 现在,我成功地设置了自定义通知,但如何处理通知按钮及其事件,如播放/暂停、上一个和下一个。。。我也尝试使用广播接收器,但没有得到任何回应 寻求解决方案和专家指导,请帮助我 提前感谢。您需要设置一个自定义意图操作,而不是AudioPlayerBroadcastReceiver组件

如何在android中设置播放/暂停、下一步和上一步按钮的通知

我是Android的新手&也是stackoverflow的新手。所以请容忍我

我在歌曲开始播放时设置通知,如下所示:

`

`

我创建了如下广播接收器: `

现在,我成功地设置了自定义通知,但如何处理通知按钮及其事件,如播放/暂停、上一个和下一个。。。我也尝试使用广播接收器,但没有得到任何回应

寻求解决方案和专家指导,请帮助我


提前感谢。

您需要设置一个
自定义意图操作,而不是
AudioPlayerBroadcastReceiver
组件类

创建一个具有如下自定义操作名称的意图

  Intent switchIntent = new Intent("com.example.app.ACTION_PLAY");
   <receiver android:name="com.example.app.AudioPlayerBroadcastReceiver" >
        <intent-filter>
            <action android:name="com.example.app.ACTION_PLAY" />
        </intent-filter>
    </receiver>
    // instance of custom broadcast receiver
    CustomReceiver broadcastReceiver = new CustomReceiver();

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
    // set the custom action
    intentFilter.addAction("com.example.app.ACTION_PLAY");
    // register the receiver
    registerReceiver(broadcastReceiver, intentFilter); 
然后,注册
pendingent
广播
接收器

  PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 100, switchIntent, 0);
然后,为播放控件设置一个
onClick
,如果需要,为其他控件执行类似的自定义操作

  notificationView.setOnClickPendingIntent(R.id.btn_play_pause_in_notification, pendingSwitchIntent);
接下来,在
AudioPlayerBroadcastReceiver
中注册自定义操作,如下所示

  Intent switchIntent = new Intent("com.example.app.ACTION_PLAY");
   <receiver android:name="com.example.app.AudioPlayerBroadcastReceiver" >
        <intent-filter>
            <action android:name="com.example.app.ACTION_PLAY" />
        </intent-filter>
    </receiver>
    // instance of custom broadcast receiver
    CustomReceiver broadcastReceiver = new CustomReceiver();

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
    // set the custom action
    intentFilter.addAction("com.example.app.ACTION_PLAY");
    // register the receiver
    registerReceiver(broadcastReceiver, intentFilter); 
编辑:如何为在代码中注册的广播接收器设置意图过滤器

您还可以通过注册的
广播接收器的
意图过滤器
从代码中设置
自定义操作
,如下所示

  Intent switchIntent = new Intent("com.example.app.ACTION_PLAY");
   <receiver android:name="com.example.app.AudioPlayerBroadcastReceiver" >
        <intent-filter>
            <action android:name="com.example.app.ACTION_PLAY" />
        </intent-filter>
    </receiver>
    // instance of custom broadcast receiver
    CustomReceiver broadcastReceiver = new CustomReceiver();

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
    // set the custom action
    intentFilter.addAction("com.example.app.ACTION_PLAY");
    // register the receiver
    registerReceiver(broadcastReceiver, intentFilter); 

下面是..你能帮我一下吗?我的应用程序不知怎么终止了,我找不到如何重新启动App,我的应用程序被终止了,但通知没有终止,在这种情况下该怎么办?@PriyankaChauhan你可以在
ondestory()
中关闭通知。如果你的活动被终止,通知也会被关闭。但是ondestory()如果应用程序被OShow终止,则不呼叫。如何在执行相册艺术、标题等挂起的意图后更改视图。。。?