android - listview 从在线 JSON 文件收到新数据后如何创建通知?

标签 android json listview android-notifications auto-update

我有一个在线 JSON 文件,显示最新的国家地震,我的应用程序中的 listView 适配器从该 JSON 文件接收数据,并且通过用户滑动手动刷新,但现在我添加自动及时的基础刷新模式到我的列表,问题是: 检测到新地震后如何创建系统通知?

最佳答案

编写以下代码,以便在从网络请求加载数据时在您的应用中发送系统通知。

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(getApplicationContext())
            .setContentTitle("App Name / Title Message")
            .setContentText("Description Message hat data is updated")
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_notification_icon)
            .setColor(getResources().getColor(R.color.icon_color))
            .setPriority(Notification.PRIORITY_HIGH) // will show notification even you are in app
            .setContentIntent(pIntent)
            .setAutoCancel(true)
            .build();
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_SHOW_LIGHTS;
    NotificationManager notificationManager = (NotificationManager)
            getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);

    //you can use unique identification int number for notificationID instead of 1

放置此代码块将显示通知。

关于android - listview 从在线 JSON 文件收到新数据后如何创建通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49423908/

相关文章:

android - Google Keep 如何在保存录音的同时进行语音识别?

android - 当我的应用程序进入后台时以编程方式从任务列表中删除我的应用程序(API <= 20)

sql - PostgreSQL:根据特定列将多行聚合为 JSON 数组

json - 如何使用 webpack 生成或导入多个 json 文件?

c# - .NET ListView 刷新

java - Android OnClickListener 更新同一 ListView 行中的文本

android - MapView添加Overlay后没有滚动

android - 如何在android中重命名文件?

json - 如何使用 Grails3 为 JSON API 创建文档

java - RecyclerView 无限滚动将位置移动到第一项