gpt4 book ai didi

android - 将通知从标准类型更改为自定义类型

转载 作者:行者123 更新时间:2023-11-29 21:53:01 25 4
gpt4 key购买 nike

从带有文本和图片的标准 notification 更改为带有自定义设计布局的更复杂类型需要使用 RemoteViews 类。由于自定义 View ,我没有使用 setContentTitle(),而是使用了 setContent(remoteviews) 方法。

更改为自定义 View 后,我删除了 setContent、setSmallIcon、setContentTitle 方法,但是在我这样做之后,通知再也没有出现。

如果我使用自定义 View ,我必须使用我们的 setContent() 方法是否正确?他们为什么在我删除其他方法时不起作用?

RemoteViews remoteviews = new RemoteViews("com.test.example", R.layout.custom_notifications);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(AudioService.this)
.setContent(remoteviews)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setOngoing(true);

最佳答案

自从 ICS 以来,我还没有真正接触过 Notification Builder。直到今天,当 GB 更流行时,我仍然按照老派的方式来做。示例时间:

        String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

// Setup an intent for when the user taps the notification
Intent notificationIntent = new Intent(this, SomeActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, 0);

// `icona` is the icon shown in the status bar.
Notification notification = new Notification(icona,
"Ticker Text", System.currentTimeMillis());

// These flags should be self explanatory
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONGOING_EVENT;

// This is where you select the xml for you custm view
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.custom_notification);

notification.contentView = contentView;
notification.contentIntent = contentIntent;

// Some ID number for the OS to keep track of your notification
int HELLO_ID = 123456;

// Send the notification
mNotificationManager.notify(HELLO_ID, notification);

这应该适用于每个版本的 android。仅仅是因为 Notification.Builder 是一个包装器,可以更轻松地创建状态栏通知。如果您查看 android 源代码,构建器也会调用这些方法。

关于android - 将通知从标准类型更改为自定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13966277/

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