【问题标题】:SQL and NotificationsSQL 和通知
【发布时间】:2017-03-25 18:10:28
【问题描述】:

目前我正在设置一个警报,当它被激活时会向用户显示通知。

我现在可以将我的 SQL 中的文本设置为我的通知,但它每次只是我数据库中的第一行。现在我想出了两种可能的解决方案,它们只是理论上的。

有没有一种方法可以在每次调用数据库时通知通知它会移动到下一个结果?

如上所述,我正在设置闹钟,当闹钟响起时,它会调用通知。现在,在我待定的警报意图中,我给它一个 id,你可以看到它作为 id 有没有办法将它提供给我的通知 ID,然后设置一个语句,如果通知 ID 等于或在列表中我的警报 ID 存储在我的 SQL 中,然后它将搜索要输入的正确文本。

待定意图....

 pendingIntent = PendingIntent.getBroadcast(v.getContext(), id,  receiver,PendingIntent.FLAG_UPDATE_CURRENT);

 alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);

 intentArrayList.add(pendingIntent);

广播....

MyDatabaseHandler myDatabaseHandler = new MyDatabaseHandler(context, null, null, 1);

Cursor cursor = myDatabaseHandler.getAllProducts();


// Information we are trying to acquire but can only get first result
String name = cursor.getString(cursor.getColumnIndexOrThrow("alarm_name"));
String date = cursor.getString(cursor.getColumnIndexOrThrow("alarm_date"));

// Alarm ID FROM SQL which we want to match with the NOTIFICATION ID.....
String alarm_request_code = cursor.getString(cursor.getColumnIndexOrThrow("alarm_code"));



 ////




 Log.i("The reciever is working", "perfect");

//create an intent to service

Intent service_Intent = new Intent(context, MessageService.class);
context.startService(service_Intent);

//Notification

NotificationManager notificationManager = (NotificationManagercontext.getSystemService(context.NOTIFICATION_SERVICE);



Intent moveActivity = new Intent(context, AlarmActivity.class);


moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

// Works with moveActivity to move the user back to main application.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    .setContentIntent(pendingIntent)
    .setContentTitle(name)
    .setContentText(date)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setAutoCancel(true);




    notificationManager.notify(Integer.valueOf(alarm_request_code), builder.build());



}
}

//////////////////////////////

解决一些问题

  1. 当前,每当我的闹钟响起时,都会向用户发送带有静态名称和描述的通知。

  2. 我想要完成的是,当此警报响起时,我的通知要么被赋予一个与警报匹配的 ID,要么查找警报,然后用活动警报详情。

  3. 这是我当前的布局在我的 SQLiteOpenHelper 中的样子

    private static final String DATABASE_NAME = "alarmList.db";
    public static final String TABLE_NAME = "alarm_list";
    
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_ALARMTIME = "alarm_time";
    public static final String COLUMN_AlARMDATE = "alarm_date";
    public static final String COLUMN_ALARMNAME = "alarm_name";
    
    
    // Alarm request code column for working with switch and
    public static final String COLUMN_ALARMRC = "alarm_code";
    

默认遵循 oncreate 和 update 方法

【问题讨论】:

    标签: android sql android-intent notifications android-pendingintent


    【解决方案1】:

    您可以通过几种不同的方式做到这一点。一种方法是添加last_used 日期列或use_count 列,并在每次使用记录时对其进行更新。然后,您将有一个查询,从您的表中选择最少使用的项目。

    另一种方法是将警报存储在应用程序中并在那里跟踪该信息。这样做的缺点是,除非您将计数/上次使用的信息存储出来(可能存储到外部统计文件中),否则在重新启动应用程序时会丢失这些计数。

    无论您以何种方式处理,这个概念实际上都是一样的。您需要在某处存储有关警报的使用信息。

    我认为这就是您所要求的,但是在重新阅读您的问题几次之后,我不太确定。也许您可以提供一些表结构示例和这些表中的一些数据,并提供一个您正在尝试完成的示例。如果我在回答中的内容不是您要找的内容,请不要担心。我要么更新它以更符合你的需要,要么完全删除它。只是想确保它是您正在寻找的东西。

    更多关于last_used查询的信息,根据要求:

    您可能希望编写类似于以下内容的查询:

    select alert_id, alert_group, alert_text
    from alerts
    where alert_group = @parm_alert_group
    order by last_used asc
    limit 1 -- this may be different depending on the flavor of DBMS used
    -- Oracle does not have "limit", but can be reproduced in other ways.
    ;
    

    然后,当您使用该警报项时,您可能希望使用当前日期时间更新该记录,以便下次您的选择查询返回最长以前使用的记录。

    update alerts set last_used = now() where alert_id = @parm_alert_id;
    

    您当然可以在返回警报信息并在返回之前更新last_used 列的函数中执行这两个操作,但这只是附加功能。相同的想法是使用两个单独的调用或单个调用。

    【讨论】:

    • 嗨,我明白你的意思,过去几个小时我一直在努力解决这个问题。目前,当我设置警报时,我已经告诉我的应用程序存储与该警报相关的 ID。您能否扩展您上次使用的查询以及我将如何实现它?也感谢您的宝贵时间。
    • @Steven - 为您添加了更多信息。
    • 我认为我们在正确的轨道上我已经更新了上面的部分以尝试澄清问题。
    猜你喜欢
    • 1970-01-01
    • 2012-08-25
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 2022-10-21
    • 1970-01-01
    相关资源
    最近更新 更多