在通知可点击自定义视图在Android 2.3或更低自定义、视图、更低、通知

2023-09-06 04:00:40 作者:独家记忆

我创建了一个自定义通知的布局,其中有一个可点击的按钮。工作至今罚款的Andr​​oid 3.0(API等级11)或更高,但不要对Android 2.3的工作, ContentIntent 通知总是覆盖我的布局,并且不能被覆盖。 我无法点击的观点,我总是单击通知并启动

I created a custom notification layout, which have a clickable button. Works so far fine on Android 3 (API Level 11) or higher, but don't work on Android 2.3, the ContentIntent from the Notification always overlays my layout and can't be overriden. I can't click the view, I always click the Notification and start the

code,用于显示通知和布局:

Code for displaying notification and layout:

Builder builder = new NotificationCompat.Builder(getApplicationContext());

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
        contentView.setImageViewResource(R.id.notImage, R.drawable.stat_icon);
        contentView.setTextViewText(R.id.notTitle, "Title");
        contentView.setTextViewText(R.id.notText, "Text");
        contentView.setOnClickPendingIntent(R.id.notButton, secondPendingIntent);
        contentView.setOnClickPendingIntent(R.id.notContentLayout, pendingIntent);

        builder
                .setContentTitle("Title")
                .setContentText("Text")
                .setSmallIcon(R.drawable.stat_icon)
                .setOngoing(true)
                .setWhen(0)
                .setTicker("Ticket")
                .setContent(contentView)
                .setContentIntent(contentIntent); //this intent override my contentView.setOnClickPendintIntent. I can't click the view. 

        not = builder.build();

        not.contentView = contentView;

布局:

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <RelativeLayout
        android:id="@+id/notContentLayout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/notButton" >

        <ImageView
            android:id="@+id/notImage"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="23dp"
            android:contentDescription="@string/image_desc" />

        <TextView
            android:id="@+id/notTitle"
            style="@style/NotificationTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/notImage" />

        <TextView
            android:id="@+id/notText"
            style="@style/NotificationText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/notTitle"
            android:layout_toRightOf="@id/notImage" />
    </RelativeLayout>

    <ImageButton
        android:id="@+id/notButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@color/transparent"
        android:contentDescription="@string/image_desc"
        android:padding="10dp"
        android:src="@android:drawable/ic_media_pause" />

</RelativeLayout>

容易。但是,不要在Android 2.3或更低,任何工作思路?

Easy. But don't work on Android 2.3 or lower, any ideas?

推荐答案

我发现它,它是不可能的了Android 2.3或更低。 Clickably 通知按钮只有在Android 3或更高的工作。

I found it it, it is just not possible with Android 2.3 or lower. Clickably Notification buttons work only on Android 3 or higher.