Android 自定义通知布局与 RemoteViews

2024-04-13

我正在尝试使用此为我的 Android 应用程序创建自定义通知post https://stackoverflow.com/questions/18367631/change-notification-layout,我偶然发现了一个我在过去 2 小时内试图解决的问题。

仅显示我的布局的图像视图,我不知道如何使其按预期工作。

My code:

package be.ac.ucl.lfsab1509.cumulus.services;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.widget.RemoteViews;
import be.ac.ucl.lfsab1509.cumulus.R;
import be.ac.ucl.lfsab1509.cumulus.activities.MainActivity;
import be.ac.ucl.lfsab1509.cumulus.entities.Song;

public class CumulusNotification {

private CumulusService mCtx;
private CumulusMediaPlayer mPlayer;
private Notification mNotification;
private NotificationCompat.Builder mBuilder;
private RemoteViews mContentView;

public CumulusNotification(CumulusService ctx, CumulusMediaPlayer player){
    mCtx = ctx;
    mPlayer = player;

    mBuilder = new NotificationCompat.Builder(
            mCtx);
    mBuilder.setSmallIcon(R.drawable.ic_stat_cumulus_notification);
    //mBuilder.setContentTitle("Cumulus is playing");
    //mBuilder.setTicker("Cumulus");
    //mBuilder.setContentText(mCtx.getCurrentSong().title());

    Intent resultIntent = new Intent(mCtx, MainActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    resultIntent.setAction(Intent.ACTION_MAIN);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(mCtx);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    mContentView = new RemoteViews(mCtx.getPackageName(), R.layout.statusbar);
    mContentView.setImageViewResource(R.id.notifimage, R.drawable.cumulus_icon);
    mContentView.setTextViewText(R.id.notiftitle, "Custom notification");
    mContentView.setTextViewText(R.id.notiftext, "This is a custom layout");
    mBuilder.setContent(mContentView);
}

public void startNotification() {
    mNotification = mBuilder.build();
    //mNotification.contentView = mContentView;
    mCtx.startForeground(R.integer.NOTIFICATION_ID, mNotification);
}

public void updateSong(Song song){
    mBuilder.setContentText(song.title());
    mNotification = mBuilder.build();
    mCtx.startForeground(R.integer.NOTIFICATION_ID, mNotification);
}
}

xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:gravity="left" >
<ImageView android:id="@+id/notifimage"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="10dp" />
<TextView android:id="@+id/notiftitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/notifimage" />
<TextView android:id="@+id/notiftext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/notifimage"
    android:layout_below="@id/notiftitle" />
</RelativeLayout>

谢谢您的帮助


问题是您没有设置正确的 contentView。

mBuilder.setContent(contentView);

但你的远程视图是mContentView。将其更改为

mBuilder.setContent(mContentView);

这是一个示例

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher);

    RemoteViews mContentView = new RemoteViews(getPackageName(), R.layout.test_tt);
    mContentView.setImageViewResource(R.id.image, R.drawable.ic_action_search);
    mContentView.setTextViewText(R.id.title, "Custom notification");
    mContentView.setTextViewText(R.id.text, "This is a custom layout");
    mBuilder.setContent(mContentView);

    startForeground(1001, mBuilder.build());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 自定义通知布局与 RemoteViews 的相关文章

随机推荐

  • 下标迭代器内的 lambda

    在下标运算符中使用 lambda 似乎不适用于 g 和 clang 这是 C 标准中的实现错误还是 不愉快 的规则 Example class A public template
  • XAML 绑定到属性

    我的 XAML C Windows 应用商店应用程序中有复选框 我还有 bool 属性 WindowsStoreTestApp SessionData RememberUser这是公共的和静态的 我想要复选框的属性IsChecked与此 b
  • jQuery 中的多个 AJAX 请求

    我有一个函数 可以从两个位置提取数据并将返回的内容放置在向用户显示的模式对话框上 这两个请求都是异步的 因为它们是跨域的 问题在于我不想在两个请求完成加载之前显示模式 在加载模式之前 如何检查以确保两个请求都已完成 我尝试将 openMod
  • 无法找到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/batch]

    情况 我正在使用 Spring Batch 为我们的数据仓库构建累积快照 但我遇到了一个我无法弄清楚的配置障碍 我使用 STS SpringSource Tool Suite 2 8 1 创建了一个简单的 Spring Batch 项目春季
  • Keras 错误:调用 Predict_on_batch 时“优化循环失败:已取消:操作已取消”

    我有一些使用 keras 的旧工作代码 我最近把它掸掉并尝试使用它 但使用的是当前版本的 keras tensorflow 调用 Predict on batch 时收到警告 错误 W tensorflow core data root d
  • ValidateRequest = False 但实际上它仍然是 True 并且忽略了它?

    我想禁用请求验证特别是在ASP NET MVC 2 0 RTM 所以我添加了一些必要的查看页面指令部分 如下所示 but 请求验证不是残疾人 我还添加了请求验证属性控制器中的相关操作如下 System Web Mvc ValidateInp
  • ASP.NET MVC 中的 RouteLink 和 ActionLink 有什么区别?

    我认为标题已经概括了这一点 有什么区别RouteLink and ActionLink 在 ASP NET MVC 中 即你什么时候使用Html RouteLink 你什么时候使用Html ActionLink 在你看来 操作和路由不必具有
  • 如何诊断护照真伪?

    由于该函数没有返回值 我如何确定导致该函数失败的原因 我有以下代码 function test req IncomingMessage res ServerResponse next err any gt void passport aut
  • 在jquery中创建循环背景动画

    我想要的是 当页面加载时 背景颜色为红色 10 秒后 bgColor 变为绿色 并带有淡入淡出动画 再过 10 秒后 它会变成橙色 然后又变成红色 依此类推 有人可以帮忙吗 Use 设置间隔 http www w3schools com j
  • Firestore - 如何在数据库中自动减少整数值?

    Firestore 最近推出了一项新功能 可以自动递增和递减整数值 我可以使用增加整数值 例如 FieldValue increment 50 但如何减量呢 我尝试使用FieldValue decrement 50 但FieldValue中
  • 在 C++ 中旋转图像而不使用 OpenCV 函数

    描述 我正在尝试在不使用 C 中的 OpenCV 函数的情况下旋转图像 旋转中心不必是图像的中心 它可能是不同的点 偏离图像中心 到目前为止 我遵循各种来源进行图像插值 我知道source https stackoverflow com a
  • 检测节点的系统路径上是否存在可执行文件

    Question 有没有一种简单的方法可以使用节点判断系统可执行文件在系统路径上是否可用 例如 如果用户安装了 python usr bin python and usr bin is in PATH我如何在 Node 中检测到这一点 相反
  • 在 Filter 内执行并呈现 JSP

    我有一个包含页面顶部内容的 JSP 我们将其称为 header jsp 出于性能原因 我想在构建页面的其余部分之前呈现此 JSP 并将其刷新给用户 看here http developer yahoo com performance rul
  • 元素不可点击错误 Ruby / Watir

    在我的测试中 我尝试访问 etsy com 进行搜索 单击结果 然后将商品添加到我的购物车 我可以做所有事情 直到我尝试单击 添加到购物车 按钮 下面的代码实际上在 IRB 中工作 所以我知道我的定位器是可靠的 但是当我运行测试时 我得到一
  • 在 Elasticsearch 中过滤折叠结果

    我有一个弹性搜索索引 其中包含表示给定时间点实体的文档 当实体更改状态时 会创建带有时间戳的新文档 当我需要获取所有实体的当前状态时 我可以执行以下操作 GET https 127 0 0 1 9200 myindex search col
  • .NET RIA 服务与 MVC 风格存储库?

    我有一个包含多个项目的解决方案 包括两个共享位于外部程序集中 也在同一解决方案中 的存储库和模型的 asp net mvc 项目 本质上 Core 存储库 楷模 国内 Web 基本MVC站点 引用核心项目 国际网 基本MVC站点 引用核心项
  • 使用 NativeMessaging 进行边缘扩展

    我有一个具有本机消息传递支持的边缘扩展 它与系统中运行的 uwp 应用程序进行通信 是否必须将扩展打包到 uwp 应用程序的 appx 文件夹中 如果没有 那么我们如何将扩展上传到边缘扩展存储中 我遵循 secureInput 示例 htt
  • 快速向 SQL Server 插入 200 万行

    我必须从文本文件中插入大约 200 万行 通过插入 我必须创建一些主表 将如此大的数据集插入 SQL Server 的最佳且快速的方法是什么 我认为最好读取 DataSet 中文本文件的数据 试用SQL批量复制 从 C 应用程序批量插入 S
  • GSON反序列化:如何知道对象?

    我尝试使用 gson 库来反序列化发送给我的对象流 在我见过的所有示例中 当调用 fromJson 方法时 我们已经知道我们期望拥有什么类型的对象 就我而言 我收到不同对象的流 我想知道在反序列化对象之前了解对象类的最佳方法 A B B1
  • Android 自定义通知布局与 RemoteViews

    我正在尝试使用此为我的 Android 应用程序创建自定义通知post https stackoverflow com questions 18367631 change notification layout 我偶然发现了一个我在过去 2