如何设置通知与播放/暂停,下一个和上一个按钮在Android。

我是Android&也在堆栈溢出。所以请跟我一起。

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

`

@SuppressLint("NewApi")

public void setNotification(String songName){

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager notificationManager = (NotificationManager) getSystemService(ns);

@SuppressWarnings("deprecation")

Notification notification = new Notification(R.drawable.god_img, null, System.currentTimeMillis());

RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification_mediacontroller);

//the intent that is started when the notification is clicked (works)

Intent notificationIntent = new Intent(this, AudioBookListActivity.class);

PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.contentView = notificationView;

notification.contentIntent = pendingNotificationIntent;

notification.flags |= Notification.FLAG_NO_CLEAR;

//this is the intent that is supposed to be called when the button is clicked

Intent switchIntent = new Intent(this, AudioPlayerBroadcastReceiver.class);

PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);

notificationView.setOnClickPendingIntent(R.id.btn_play_pause_in_notification, pendingSwitchIntent);

notificationManager.notify(1, notification);

}

`

我创建BroadcastReceiver像下面:

`

private class AudioPlayerBroadcastReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

System.out.println("intent action = " + action);

long id = intent.getLongExtra("id", -1);

if(Constant.PLAY_ALBUM.equals(action)) {

//playAlbum(id);

} else if(Constant.QUEUE_ALBUM.equals(action)) {

//queueAlbum(id);

} else if(Constant.PLAY_TRACK.equals(action)) {

//playTrack(id);

} else if(Constant.QUEUE_TRACK.equals(action)) {

//queueTrack(id);

} else if(Constant.PLAY_PAUSE_TRACK.equals(action)) {

// playPauseTrack();

System.out.println("press play");

} else if(Constant.HIDE_PLAYER.equals(action)) {

// hideNotification();

System.out.println("press next");

}

else {

}

}

}`

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

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

提前致谢。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐