1

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

更改为自定义视图后,我删除了setContent, setSmallIcon,and setContentTitle mehods,但是在我这样做之后,通知就再也没有出现过。

如果我使用的是自定义视图,那么我必须使用 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);
4

1 回答 1

2

好吧,自从 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是一个包装器,可以更轻松地创建状态栏通知。如果您查看 androids 源代码,构建器也会调用这些方法。

于 2012-12-20T06:45:42.323 回答