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

标签 android mediacontroller android-music-player

我正在创建一个音乐播放器应用程序。当我的应用程序在后台运行时,我想在通知栏上显示媒体 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/

相关文章:

android - Google Play 游戏服务 + BaseGameUtils 添加到 Gradle 项目 = 包不存在

android - fragment 中奇怪的 onPause()、onResume() 行为

mediacontroller - 向 MediaController 添加按钮

android - ExoPlayer 和启动/暂停/seekTo 命令

java - 从路径获取mp3文件

android - 从 Android AppWidget 播放/控制歌曲

c# - 检测用户是否正在从另一个应用程序播放音乐

java - android sherlock setDisplayHomeAsUpEnabled 不工作以及如何设置自定义目标?

Android:GPS 丢失修复

javascript - 视频循环自动播放不适用于 Chrome/safari(webkit 错误)