【问题标题】:how to automatically set a reminder which data is from the database?如何自动设置提醒哪些数据来自数据库?
【发布时间】:2012-09-27 20:19:07
【问题描述】:

源代码:

    private void saveState() {

    DatabaseHelper myDbHelper = new DatabaseHelper(ReminderEditActivity.this);
    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }
    try {
        myDbHelper.openDataBase();
    }catch(SQLException sqle){
        throw sqle;
    }
    c=myDbHelper.query("tblmain", null, null, null, null,null, null);
    if(c.moveToFirst())
    {
        do {
                    mRowId = c.getLong(0);
                    String datetime = c.getString(8);

                    SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
                    Date date = null;
                    try {
                        date = dateTimeFormat.parse(datetime);
                        mCalendar.setTime(date); 
                    } catch (ParseException e) {
                        Log.e("ReminderEditActivity", e.getMessage(), e); 
                    } 
    mCalendar.setTime(date);

    new ReminderManager(this).setReminder(mRowId, mCalendar); 
        } while (c.moveToNext());
    }
}

提醒管理器

public class ReminderManager {

private Context mContext; 
private AlarmManager mAlarmManager;

public ReminderManager(Context context) {
    mContext = context; 
    mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}

public void setReminder(Long taskId, Calendar when) {

    Intent i = new Intent(mContext, OnAlarmReceiver.class);
    i.putExtra(RemindersDbAdapter.KEY_eventid, (long)taskId); 

    PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT); 

    mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}
}

onalarmreceiver

public class OnAlarmReceiver extends BroadcastReceiver {

private static final String TAG = ComponentInfo.class.getCanonicalName(); 


@Override   
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "Received wake up from alarm manager.");

    long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_eventid);

    WakeReminderIntentService.acquireStaticLock(context);

    Intent i = new Intent(context, ReminderService.class); 
    i.putExtra(RemindersDbAdapter.KEY_eventid, rowid);  
    context.startService(i);

}
}

提醒服务

public class ReminderService extends WakeReminderIntentService {

public ReminderService() {
    super("ReminderService");
        }

@Override
void doReminderWork(Intent intent) {
    Log.d("ReminderService", "Doing work.");
    Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_eventid);

    NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, ReminderEditActivity.class); 
    notificationIntent.putExtra(RemindersDbAdapter.KEY_eventid, rowId); 

    PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 

    Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis());
    note.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi);
    note.defaults |= Notification.DEFAULT_SOUND; 
    note.flags |= Notification.FLAG_AUTO_CANCEL; 

    // An issue could occur if user ever enters over 2,147,483,647 tasks. (Max int value). 
    // I highly doubt this will ever happen. But is good to note. 
    int id = (int)((long)rowId);
    mgr.notify(id, note); 
}
}

我这里有这段代码,它从资产文件夹复制数据库,然后将数据设置为将用于添加通知的局部变量。我想自动设置数据来自数据库的提醒,但通知会继续读取单击按钮时的日期和时间,而不是数据库中的日期和时间,因此程序的结果是它触发通知和单击按钮后立即显示通知..请帮助我。

我想根据数据库中的日期和时间设置通知。请帮助我如何做到这一点。

【问题讨论】:

  • 请回答。 :'( 我真的需要你的帮助。

标签: android sqlite reminders notificationmanager


【解决方案1】:

你有没有试过在这里设置断点

 String datetime = c.getString(8);

并查看 datetime 变量中保存的时间?然后你可以查明时间出错的地方。您正在获取日期的毫秒时间,但我认为您需要从中减去当前时间才能获得提醒时间(时间 = when- System.currentTimeInMillis)。

【讨论】:

  • datetime中存储的数据就是这种格式-> "MM-dd-yyyy kk:mm:ss"。在 String datetime = c.getString(8); 之后添加断点是什么意思??关闭数据库?
  • 如果提醒设置是通过日期选择器和时间选择器完成的,项目运行正常,但是当我更改代码以根据数据库中的数据自动设置它时,通知没有显示从数据库中设置的日期和时间,而不是在单击按钮后立即显示。
  • 用于调试。我不知道你用的是什么编辑器,我用的是eclipse,但是在调试的时候就在那里设置一个断点,你可以看到变量的值是什么。如果它是假设的日期,那么您知道您正在正确地从数据库中检索它并且问题在代码中更远。如果它不是存储在数据库中的日期,那么您正在错误地存储和/或检索它。如果这没有意义或者您还有其他问题,请告诉我
  • 我也使用 Eclipse。当我敬酒日期date = dateTimeFormat.parse(datetime); 时,它显示 Fri Sept 14 08:00:00 GMT+08:00 2012 而数据库中对应的日期是 09-14-2012 08:00:00 这是否意味着我使用的格式是错还是什么?
  • 不,格式正确,但您不能将日期设置为过去的一天。您需要一个条件语句,例如 if date
猜你喜欢
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多