gpt4 book ai didi

android - 如何将媒体 Controller 按钮放在通知栏上?

转载 作者:IT老高 更新时间:2023-10-28 22:02:58 24 4
gpt4 key购买 nike

我正在创建一个音乐播放器应用程序。当我的应用程序在后台运行时,我想在通知栏上显示媒体 Controller 。它看起来像 Google 播放器。

enter image description here

如何做到这一点?

最佳答案

这是上面对新 API 正确完成的示例

在你的 main 中,当你想启动通知时实例化类:

NotificationPanel nPanel = new NotificationPanel(MyActivity);

当你想取消通知时:(因为它是一个正在进行的通知)

nPanel.notificationCancel();    

然后为通知调用者创建类:

public class NotificationPanel {

private Context parent;
private NotificationManager nManager;
private NotificationCompat.Builder nBuilder;
private RemoteViews remoteView;

public NotificationPanel(Context parent) {
// TODO Auto-generated constructor stub
this.parent = parent;
nBuilder = new NotificationCompat.Builder(parent)
.setContentTitle("Parking Meter")
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true);

remoteView = new RemoteViews(parent.getPackageName(), R.layout.notificationview);

//set the button listeners
setListeners(remoteView);
nBuilder.setContent(remoteView);

nManager = (NotificationManager) parent.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(2, nBuilder.build());
}

public void setListeners(RemoteViews view){
//listener 1
Intent volume = new Intent(parent,NotificationReturnSlot.class);
volume.putExtra("DO", "volume");
PendingIntent btn1 = PendingIntent.getActivity(parent, 0, volume, 0);
view.setOnClickPendingIntent(R.id.btn1, btn1);

//listener 2
Intent stop = new Intent(parent, NotificationReturnSlot.class);
stop.putExtra("DO", "stop");
PendingIntent btn2 = PendingIntent.getActivity(parent, 1, stop, 0);
view.setOnClickPendingIntent(R.id.btn2, btn2);
}

public void notificationCancel() {
nManager.cancel(2);
}
}

然后添加接受待处理 Intent 的返回类:

public class NotificationReturnSlot extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String action = (String) getIntent().getExtras().get("DO");
if (action.equals("volume")) {
Log.i("NotificationReturnSlot", "volume");
//Your code
} else if (action.equals("stopNotification")) {
//Your code
Log.i("NotificationReturnSlot", "stopNotification");
}
finish();
}
}

然后您需要为按钮制作一个 XML 文件。这是一个简单的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="volume" />

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Stop" />

<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/msglbl" />

最后也是最重要的,Manifest 文件:

   <activity android:name=".NotificationReturnSlot"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"/>

关于android - 如何将媒体 Controller 按钮放在通知栏上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12526228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com