0

我想创建提醒。我正在使用下面的代码在用户设置的特定时间和日期生成通知。

public class MainActivity extends Activity {
TimePicker timePicker;
DatePicker datePicker;
Button button1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     button1 = (Button) findViewById(R.id.button1);

     button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
             timePicker = (TimePicker) findViewById(R.id.timePicker1);
                datePicker = (DatePicker) findViewById(R.id.datePicker1);    
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);     

                Calendar calendar = Calendar.getInstance();     


                calendar.set(Calendar.YEAR, datePicker.getYear());
                calendar.set(Calendar.MONTH, datePicker.getMonth());
                calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());                 
                calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
                calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
                calendar.set(Calendar.SECOND, 0);

                Intent inn = new Intent(MainActivity.this,AlaramDetail.class);
                PendingIntent displayIntent = PendingIntent.getActivity(
                        getBaseContext(), 0,inn, 0);       
                alarmManager.set(AlarmManager.RTC_WAKEUP, 
                        calendar.getTimeInMillis(), displayIntent);



        }
    });

}
}

下面是具有生成通知代码的 AlaramDetail 类。

public class AlaramDetail extends Activity {

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail);
    NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(android.R.drawable.stat_notify_more, "This is important notification",System.currentTimeMillis());
    String title = "pay attention";
    String detail = "this is implortant";
    Intent intent = new Intent(AlaramDetail.this,AlaramDetail.class);
    finish();
    PendingIntent pintent = PendingIntent.getActivity(AlaramDetail.this, 0,intent ,0 );

    notification.setLatestEventInfo(AlaramDetail.this, title, detail, pintent);

    notification.flags=Notification.FLAG_AUTO_CANCEL;
    notification.defaults|=Notification.DEFAULT_SOUND;
    notification.defaults|=Notification.DEFAULT_LIGHTS;
    notification.defaults|=Notification.DEFAULT_VIBRATE;
    nm.notify(1, notification);
}   

} 

上面的代码工作正常。完成此操作后,我创建了一个数据库,该数据库存储用户输入的多个日期和时间。数据库将日期和时间存储为字符串。现在我想在存储在数据库中的特定日期和时间生成通知。我不知道如何传递日期和从数据库到 calendar.set(int field,int value) 命令的时间。我想我可以使用游标来检索日期和时间,但我不知道在哪里和哪里传递什么值 args。我的数据库有三列名称,日期,时间。

4

1 回答 1

1

最好在数据库中以毫秒为单位存储时间......它会给你更好的性能......

检索数据后,您只需使用此代码设置时间

   Calendar calender = Calendar.getInstance();
   calender.setTimeInMillis(TimeInMilli); 
于 2013-08-24T10:04:46.053 回答