从android中点击通知获取数据

2023-12-14

嘿伙计们,我需要帮助如何从使用广播接收器设置的待处理意图中获取数据。我想要发生的是在单击通知时获取我的活动所需的 id 数据。

这就是我制作额外内容的方式

public class AlertReceiver extends BroadcastReceiver {

    private int id;
    // Called when a broadcast is made targeting this class
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        String title = bundle.getString("title");
        String time = bundle.getString("time");
        id = bundle.getInt("id");

        createNotification(context, title, time, "Pharoah Reminder");
    }

    public void createNotification(Context context, String msg, String msgText,  String msgAlert){
        Intent reminderActivity =  new Intent(context, ReminderPreviewActivity.class);
        reminderActivity.putExtra("id", id);

        PendingIntent notificationIntent = PendingIntent.getActivity(context, id,
                reminderActivity, PendingIntent.FLAG_UPDATE_CURRENT );

        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(msg)
                .setTicker(msgAlert)
                .setContentText(msgText)
                .setContentIntent(notificationIntent);

        mBuilder.setContentIntent(notificationIntent);
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);

        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(id, mBuilder.build());
    }
}

但当我尝试从通知中打开我的活动时,它始终为空。

这是我如何得到它的。

public class ReminderPreviewActivity extends AppCompatActivity {

    private Toolbar mToolBar;

    private TextView titleTextView;
    private TextView descTextView;
    private TextView timeTextView;
    private TextView dateTextView;

    private String title;
    private String desc;
    private String date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminder_preview);

        mToolBar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);



        titleTextView   = (TextView) findViewById(R.id.titleTextView);
        descTextView    = (TextView) findViewById(R.id.descTextView);
        timeTextView    = (TextView) findViewById(R.id.timeTextView);
        dateTextView    = (TextView) findViewById(R.id.dateTextView);

        Intent extras = getIntent();

        if(extras.getStringExtra("title") != null){
            setContentFromExtras(extras);
        }else{
            setContentFromDB(extras);
        }
    }

    private void setContentFromExtras(Intent extras){
        title   = extras.getStringExtra("title");
        desc    = extras.getStringExtra("desc");
        date    = extras.getStringExtra("date");

        String[] dateDB = date.split(" ");

        titleTextView.setText(title);
        descTextView.setText(desc);
        timeTextView.setText(formatTime(dateDB[1])+" "+dateDB[2]);
        dateTextView.setText(formatDate(dateDB[0]));
    }

    public void setContentFromDB(Intent extras){
        String id = extras.getStringExtra("id");

        int reminderID = Integer.parseInt(id);

        titleTextView.setText(reminderID);
    }

我需要 id 来从数据库检索数据。当我关闭应用程序时,也会发生同样的情况。


In your AlertReceiver,你已经声明了

private int id;

你用这个int值在

reminderActivity.putExtra("id", id);

所以你还必须把它作为int在你的setContentFromDB() method:

int reminderID = extras.getIntExtra("id", someInt);
titleTextView.setText("" + reminderID);

其中 'someInt' 应该是int通常从不使用的值或默认值(如果这对您的情况有意义)。

你从getStringExtra("id")因为如果没有的话那就是返回值String找到了密钥“id”。

如果你使用int价值与TextView.setText(),它将被解释为字符串资源。我认为在你的情况下(“id”用于数据库)这很糟糕。

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

从android中点击通知获取数据 的相关文章

随机推荐

  • 如何在 Javascript 中从 http 源返回图像

    我有一个包含数据的字典列表 其中每个字典都有一个键 图片 和一个作为图像源 http 的值 picture http 以及其他键 值集 我需要返回一个 html 页面 其中包含与词典关联的名称和图像列表 我已设法返回名称列表 但是当我尝试返
  • MySQL 中复合唯一键是否有索引?

    我有一个UserSkills具有三列的表 id PK userId FK and skillId FK 我想对以下组合强制执行复合唯一约束userId and skillId 为了更快地查找 我希望启用复合索引 userId skillId
  • 使用正则表达式以任意顺序匹配多个单词[重复]

    这个问题在这里已经有答案了 好吧 假设我想匹配一个句子中的 3 个单词 但我需要以任何顺序匹配它们 例如 sentences Array one two three four five six seven eight nine ten te
  • 同一桌子导轨的多个关联

    我有两个类 User 和 Bug bug 中有两个外键引用 user id 问题是我在创建记录时如何将 user id 存储在外键列中 例如 如果用户输入 bug 那么他的 id 存储在buger id 列 类错误 belongs to b
  • C: char* 问题

    code c TASK Reverse a string by reversing pointers Function should use return type char and use a char parameter as inpu
  • 清单中的广播接收器注册与活动

    我需要一些帮助来了解我的广播接收器何时可以在清单中注册时正常工作 而不是必须从正在运行的活动或服务中注册 因此 例如 如果我使用以下意图过滤器注册一个独立接收器 则它可以在没有服务 活动引用的情况下工作
  • 使用 gekko 进行 Python 优化

    我第一次使用 gekko 来对 python 进行优化 我对 python 没有太多经验 但我知道基础知识 运行优化时出现错误代码 13 import Gekko optimization package from gekko import
  • 具有键“XXX”的 ViewData 项的类型为“System.Int32”,但必须为“IEnumerable”类型

    我有以下视图模型 public class ProjectVM Display Name Category Required ErrorMessage Please select a category public int Category
  • 高效使用 pdftools 包中的 pdf_data 函数

    最终目标是使用 pdftools 包有效地浏览一千页 pdf 文档 以一致 安全地生成可用的数据框 标题 我尝试使用 tabulizer 包和 pdf text 函数 但结果不一致 因此 开始通过pdf data 功能 我比较喜欢 对于那些
  • 是否有任何浏览器布局引擎可以本地解释 SASS(没有 CSS)或启用此功能的插件?

    想象一个仅由 HTML SASS 文件组成的网站 但在浏览器中显示就像 HTML CSS 一样 本质上 首先跳过将 SASS 编译为 CSS 的步骤 有没有任何 甚至是边缘 项目可以实现这一点 不 但是我们正在努力将想法从 Sass Les
  • 语法错误:预期表达式,得到“<”

    I got SyntaxError expected expression got lt 当我执行以下节点代码时控制台出现错误 var express require express var app express app all func
  • 如何使用 jquery 对 api 进行 jsonp 调用

    一般来说 我对编程很陌生 当我调用 moviedb org api 时 我无法将数据导入到我的 Web 应用程序中 我正在使用jquery 我已经阅读了所有文档 甚至是食谱 我仍然在努力完成这项工作 我还检查了我的谷歌开发工具控制台 它显示
  • 在 swift Playground 中使用 NSTimer [重复]

    这个问题在这里已经有答案了 我想知道如何使用NSTimer在 Swift Playground 内 这个问题之前已经被问过 但没有一个答案真正回答了问题 这是我的游乐场代码 import Foundation class MyClass f
  • Apache 时间戳不正确

    我正在使用运行 PHP 的 WAMP 服务器 在特定步骤中 我尝试捕获系统时间并使用以下查询将其添加到数据库中 strSQLInsert UPDATE track SET State Repeat DateTime date m d Y h
  • 在 IntelliJ IDEA 和 Gradle 上调试时等待用户输入

    我有最简单的 Java 应用程序 如果从命令行执行 它就可以工作 但如果我想通过 IntelliJ IDEA 14 Ultimate 进行调试 System in read 部分总是返回 1 无需在其中输入任何内容 import java
  • 给定值 x 和 y,如果为 true,则返回规则名称

    这是我的序言文件 male bob male john female betty female dana father bob john father bob dana mother betty john mother betty dana
  • ColdFusion 相当于 PHP hash_hmac

    key 12345678876543211234567887654321 iv 1234567887654321 plaindata This is a test string enc base64 encode mcrypt encryp
  • 如何使用 System.Drawing 绘制表格

    我想使用 System Drawings 绘制一个表格 然后用一些文本填充单元格 该文本每隔几秒钟就会在不同的时刻发生变化 这是一个游戏 其中有一个网格 每隔几秒钟 随机单元格就会显示一个数字 然后用户必须在其下面的文本框中输入答案 此外
  • R中字符类的计数函数

    我的代码正常工作 然后 RStudio 崩溃了 当我重新打开它时 我的一行代码现在不起作用 CodeTable lt count unique Data Code 以前 这创建了一个包含 3 列的简单数据库 1 数字顺序 2 唯一代码 3
  • 从android中点击通知获取数据

    嘿伙计们 我需要帮助如何从使用广播接收器设置的待处理意图中获取数据 我想要发生的是在单击通知时获取我的活动所需的 id 数据 这就是我制作额外内容的方式 public class AlertReceiver extends Broadcas