简体   繁体   中英

How to retrive date and time from database and create notification on that particular date

I want to create Reminder.I am using below code to generate notification at a particular time and date set by the user.

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);



        }
    });

}
}

Below is AlaramDetail class that have code for genrating notification.

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);
}   

} 

above code is working correctly. After doing this I have created a database that store multiple date and time inputted by the user.database store date and time as string.now i want to generate notification at particular date and time stored in database.I dont know how to pass date and time from database to calendar.set(int field,int value) commands.I think i can use cursor for retrieving date and time but i dont know what value to pass in where and where args.My database has three columns name,date,time.

Also better to do with storing Time in milliseconds as long in database...it will give you better performance...

After Retrieving data you just set time using this code

   Calendar calender = Calendar.getInstance();
   calender.setTimeInMillis(TimeInMilli); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM