我正在尝试制作一个在 api 级别 8 上工作但具有与标准音乐播放器相同的通知功能的音乐播放器,即控制播放器的 3 个按钮。这是我的代码,我知道它还没有真正完成,但现在我只想弄清楚如何使按钮可点击。如果我尝试单击任何按钮,它只会忽略它们,就好像我单击了整个通知一样。
RemoteViews views = new RemoteViews(getPackageName(),
R.layout.not_rview);
views.setTextViewText(R.id.textView1, nowPlaying.title);
Intent intentP1 = new Intent(this, MainActivity.class);
intent.setAction("YOUR_AWESOME_ACTION");
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0, intentP1, 0);
views.setOnClickPendingIntent(R.id.button1, pendingIntent1);
Intent intentP2 = new Intent(this, MainActivity.class);
intent.setAction("YOUR_AWESOME_ACTION");
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 0, intentP2, 0);
views.setOnClickPendingIntent(R.id.button2, pendingIntent2);
Intent intentP3 = new Intent(this, MainActivity.class);
intent.setAction("YOUR_AWESOME_ACTION");
PendingIntent pendingIntent3 = PendingIntent.getBroadcast(this, 0, intentP3, 0);
views.setOnClickPendingIntent(R.id.button3, pendingIntent3);
Notification status = new Notification();
status.contentView = views;
status.flags |= Notification.FLAG_ONGOING_EVENT;
status.icon = R.drawable.play;
status.contentIntent = PendingIntent.getActivity(this, 0, new Intent(
"MusicService")
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0);
startForeground(1, status);
另外,这是我前进的正确方向还是我应该做些不同的事情?我从内置应用程序中复制了一些(大部分)代码,并从此处的其他答案中复制了一些代码。