gpt4 book ai didi

android - RemoteView 不在自定义通知中显示按钮

转载 作者:太空宇宙 更新时间:2023-11-03 10:18:06 24 4
gpt4 key购买 nike

我正在通过集成 RemoteViews 实现自定义推送通知。问题是,remoteview 中的按钮没有显示。我没有弄错我做错了什么。

代码:

public class AlarmReceiver extends BroadcastReceiver {
Bitmap bannerimage;
private static int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;

@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {


bannerimage = BitmapFactory.decodeResource(context.getResources(),
R.drawable.dummyturkey);
MY_NOTIFICATION_ID=1;
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.custom_push_layout);
Intent myIntent = new Intent(context,DoSomething.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
MY_NOTIFICATION_ID,
myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
System.out.println("Alarm fired AlarmReciever:"+mydate);

remoteViews.setImageViewBitmap(R.id.imgbanner,bannerimage);

Notification myNotification = new Notification.Builder(context)
.setContent(remoteViews)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setSound(alarmSound)
.setAutoCancel(false).build();



NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);


}

}

XML 文件 custom_push_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >

<ImageView
android:id="@+id/imgbanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@drawable/ic_launcher"
android:scaleType="fitXY" />

<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This chicken has send you a friend request" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/btnaccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accept" />

<Button

android:id="@+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/btnaccept"

android:text="Cancel" />
</RelativeLayout>

</LinearLayout>

通知来了,但它只显示 ImageView ,而不显示布局中的按钮或 TextView 。

请仅引用上述代码给出解决方案,即我做错了什么或遗漏了什么。请不要发布新代码。

最佳答案

使用 Notification.Builder(context).setContent 您可以为通知设置普通 View 布局。根据Notifications API Guide普通 View 布局高度限制为 64dp

The height available for a custom notification layout depends on the notification view. Normal view layouts are limited to 64 dp, and expanded view layouts are limited to 256 dp.

您需要做的是将 contentViewbigContentView 设置为 Notification 对象。创建两个单独的布局,一个用于普通布局,一个用于大 View 布局,并创建两个 RemoteViews

 RemoteViews customViewSmall = new RemoteViews(context.getPackageName(), R.layout.custom_notification_small);
RemoteViews customViewBig = new RemoteViews(context.getPackageName(), R.layout.custom_notification_big);

...
set the values of the views
...

Notification myNotification = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setSound(alarmSound)
.setAutoCancel(false).build();

myNotification.contentView = customViewSmall;
myNotification.bigContentView = customViewBig;

请注意 bigContentView 可从 API16 获得。同样在 UI xml 中,为所有 TextView View 添加颜色。

编辑:在 v4 支持库 24 中,NotificationBuilderCompat 具有新方法 setCustomBigContentView(),因此将 remoteViews 设置为 Notification对象,只需使用 NotificationBuilderCompat。你可以在这里看到它:Notification API Guide.

结果:

Result:

关于android - RemoteView 不在自定义通知中显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31874511/

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