Android 将通知从标准类型更改为自定义类型
从带有文本和图片的标准Android 将通知从标准类型更改为自定义类型,android,notifications,custom-controls,Android,Notifications,Custom Controls,从带有文本和图片的标准通知更改为带有自定义设计布局的更复杂类型需要使用RemoteView类。由于自定义视图,我没有使用setContentTitle(),而是使用了setContent(RemoteView)方法 更改为自定义视图后,我删除了setContent、setSmallIcon、和setContentTitle方法,但之后,通知再也没有出现 如果我使用的是自定义视图,那么我只需要使用setContent()方法,对吗?他们为什么不使用我删除其他方法 RemoteViews remot
通知更改为带有自定义设计布局的更复杂类型需要使用RemoteView类。由于自定义视图,我没有使用setContentTitle(),而是使用了setContent(RemoteView)方法
更改为自定义视图后,我删除了setContent、setSmallIcon、
和setContentTitle方法
,但之后,通知再也没有出现
如果我使用的是自定义视图,那么我只需要使用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以来,我还没有真正接触过通知生成器。即使到了今天,当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
是一个包装器,可以更轻松地创建状态栏通知。如果您查看androids的源代码,构建器也会调用这些方法。如果您没有将通知的优先级设置为最低优先级(就像Google now的天气通知一样),那么在发送通知时,您仍然需要设置一个显示在状态栏中的标记文本和图标无论通知是自定义的还是非自定义的都需要。是的,您始终需要在通知上设置图标
和内容视图
,否则它将被忽略。后者可以使用生成器
或不推荐使用的setLateStevenInfo()
和构造函数生成,也可以通过创建您自己的远程视图
(在support-v4库中)来生成使用较新的Builder
API为您完成所有这些,但它也适用于较旧版本的Android。