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

时间:2012-12-20 06:07:44

标签: android notifications custom-controls

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

更改为自定义视图后,我删除了setContent, setSmallIcon,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);

1 个答案:

答案 0 :(得分: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是一个包装器,可以更容易地创建状态栏通知。如果你看一下orroids源代码,构建器也会调用这些方法。