在Android中将控件放入通知中

在Android中将控件放入通知中,android,android-notifications,Android,Android Notifications,Spotify Android应用程序在通知中加入了音乐控件(播放/暂停,下一首歌) 我想让用户从控件内部通知控制服务。我该怎么做 为此,您需要制作自己的通知布局 你能试试下面的代码吗 首先为通知创建一个xml 自定义通知.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="

Spotify Android应用程序在通知中加入了音乐控件(播放/暂停,下一首歌)

我想让用户从控件内部通知控制
服务
。我该怎么做


为此,您需要制作自己的通知布局

你能试试下面的代码吗

首先为通知创建一个xml

自定义通知.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" >
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        style="Custom Notification Title" />
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
        style="Custom Notification Text" />
</RelativeLayout>
public class MainActivity extends Activity {

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, "Custom Notification", when);

        NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
        contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.title, "Custom notification");
        contentView.setTextViewText(R.id.text, "This is a custom layout");
        notification.contentView = contentView;

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;

        notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound

        mNotificationManager.notify(1, notification);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

向此通知添加操作。操作通常由系统显示为通知内容旁边的按钮。 在Android 4.1之前的平台上不会出现操作按钮


有没有办法在Android 4.1以下的版本中添加类似的内容?看起来你必须创建一个自定义通知布局。嗨,Sunny,你能告诉我如何在运行时使用dynamic?我可以动态地创建相对论吗?嗨,诺伊蒂达特,你可以用XML定义一个容器布局,然后在Java文件中定义。你可以使用LayoutInflater膨胀布局,将它添加到前面XML文件中定义的容器中。非常感谢Sunny,我对Java非常陌生,所以现在我刚刚开始从Java移植到JNI,我可以问一个单独的问题吗?谢谢,我知道这是一匙羹,但这是我学习的最好方式。是的,写信给我sunnyg28@gmail.com对于任何疑问,非常感谢@Sunny!