如何设置持久通知背景图像

时间:2012-11-02 17:33:33

标签: android notifications android-notification-bar

我一直在开发一个Android应用程序,我需要设置一个持久的通知,它永远不会离开状态栏。所有东西都设置正确,但我需要在通知中使用完整的背景,没有任何文字或任何东西。正如我附上截图。

Notification Background like Yandex Search app

我使用了以下代码:

String notificationTitle = "Here My App Name Goes";
        String notificationMessage = "Search with ---------";

        Intent targetIntent = new Intent(Intent.ACTION_MAIN);
        targetIntent.setClassName(this, this.getClass().getName());

        int requestCode = AppSingleton.NOTIFICATION_ID_ALWAYS;

        PendingIntent contentIntent = PendingIntent.getActivity(this,
                requestCode, targetIntent, 0);
        String statusBarTickerText = "Search from ----------";
        int icon = R.drawable.ic_launcher;

        Notification notification = new Notification(icon,
                statusBarTickerText, System.currentTimeMillis());
        notification.flags = Notification.FLAG_ONGOING_EVENT
                | Notification.FLAG_NO_CLEAR;
        notification.setLatestEventInfo(this, notificationTitle,
                notificationMessage, contentIntent);

        nm.notify(AppSingleton.NOTIFICATION_ID_ALWAYS, notification);

我可以设置我的应用程序图标并在其上面写一些文字,但我需要设置完整的背景,因为它是提供的屏幕截图。

请帮我摆脱这个项目。

1 个答案:

答案 0 :(得分:2)

我得到了解决问题的方法,只需要通过一些代码更改来制作自定义通知布局。以下是我编辑的代码:

Notification notification = new Notification(
                R.drawable.ic_launcher, "All ----",
                System.currentTimeMillis());

        RemoteViews contentView = new RemoteViews(getPackageName(),
                R.layout.notification);

        notification.contentView = contentView;
        final Intent notificationIntent = new Intent(this,
                MainActivity.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        nm = (NotificationManager) getSystemService(MainActivity.NOTIFICATION_SERVICE);

        notification.flags |= Notification.FLAG_NO_CLEAR;
        notification.flags |= Notification.FLAG_ONGOING_EVENT;

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        notification.contentIntent = contentIntent;

        nm.notify(AppSingleton.NOTIFICATION_ID_ALWAYS, notification);

正如您在上面看到的,RemoteViews类完成了我的工作而没有任何问题。刚做了一个布局并在这里引用它。而且,繁荣得到了我的通知背景。