0

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

通知背景,如 Yandex 搜索应用程序

我使用了以下代码:

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);

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

请帮助我摆脱这个 prm。

4

1 回答 1

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 类没有问题地完成了我的工作。刚刚做了一个布局并在这里引用它。而且,繁荣得到了我的通知的背景。

于 2012-11-02T18:14:26.517 回答