我正在Android中制作Reminder应用程序。为此,我通过从用户获取值并将其存储到数据库来存储日期和时间,以便在该特定时间生成通知。
我的活动如下:
4
您注意到我在Button的点击事件中调用了函数)
和a=3,b=4
。
问题是: 即使时间与从数据库获得的时间不匹配,它也会生成通知。
我的Receiver课程如下:
(3 * 4 / 4 + 4)
我的服务类如下:
public class HomeActivity extends Activity implements View.OnClickListener {
private EditText medWorkTitle;
private EditText medWorkDescription;
private Button mbtnSetTime;
private Button mbtnSetDate;
private Button mbtnSave;
private TextView mtxtDate, mtxtTime;
private RadioGroup mrgWorkValue;
private RadioButton mrbWorkValue;
private Calendar mCalendar;
private int mInt_year, mInt_month, mInt_day;
static final int DATE_PICKER_ID = 1111;
//TIME PICKER
private TimePicker timepick1;
static final int TIME_DIALOG_ID = 111;
private int hour;
private int minute;
final Calendar c = Calendar.getInstance();
////Database tasks..
private DatabaseHelper dbHelper;
private PendingIntent pendingIntent;
public ArrayList<String> strDataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
init();
dateTimeUtility();
}
public void init() {
strDataList = new ArrayList<String>();
mCalendar = Calendar.getInstance();
medWorkDescription = (EditText) findViewById(R.id.edWorkDescription);
medWorkTitle = (EditText) findViewById(R.id.edWorkTitle);
mtxtDate = (TextView) findViewById(R.id.txtDate);
mtxtTime = (TextView) findViewById(R.id.txtTime);
mbtnSave = (Button) findViewById(R.id.btnSave);
mbtnSave.setOnClickListener(this);
mbtnSetDate = (Button) findViewById(R.id.btnSetDate);
mbtnSetDate.setOnClickListener(this);
mbtnSetTime = (Button) findViewById(R.id.btnSetTime);
mbtnSetTime.setOnClickListener(this);
mrgWorkValue = (RadioGroup) findViewById(R.id.rgWorkValue);
///For Database..
dbHelper = new DatabaseHelper(this);
try {
dbHelper.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
strDataList = dbHelper.getAllRemiders();
}
public void dateTimeUtility() {
// FOR DATE-PICKER
mInt_year = mCalendar.get(Calendar.YEAR);
mInt_month = mCalendar.get(Calendar.MONTH);
mInt_day = mCalendar.get(Calendar.DAY_OF_MONTH);
showDate(mInt_year, mInt_month + 1, mInt_day);
// Current Hour
hour = mCalendar.get(Calendar.HOUR_OF_DAY);
// Current Minute
minute = mCalendar.get(Calendar.MINUTE);
// set current time into output textview
updateTime(hour, minute);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnSetDate:
setDate(view.getRootView());
break;
case R.id.btnSetTime:
showDialog(TIME_DIALOG_ID);
break;
case R.id.btnSave:
insertWork();
initCalender();
generateNotification();
break;
}
}
// /////FOR DATE-PICKER
public void setDate(View view) {
showDialog(DATE_PICKER_ID);
Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT)
.show();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case DATE_PICKER_ID:
return new DatePickerDialog(this, myDateListener, mInt_year,
mInt_month, mInt_day);
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,
false);
}
return null;
}
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
showDate(arg1, arg2 + 1, arg3);
}
};
private void showDate(int year, int month, int day) {
if (day < 10) {
if (month < 10) {
mtxtDate.setText(new StringBuilder().append(year).append("-").append("0" + month).append("-").append("0" + day));
} else {
mtxtDate.setText(new StringBuilder().append(year).append("-").append(month).append("-").append("0" + day));
}
} else {
if (month < 10) {
mtxtDate.setText(new StringBuilder().append(year).append("-").append("0" + month).append("-").append(day));
} else {
mtxtDate.setText(new StringBuilder().append(year).append("-").append(month).append("-").append(day));
}
}
}
////TIMEPICKER
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
// TODO Auto-generated method stub
hour = hourOfDay;
minute = minutes;
updateTime(hour, minute);
}
};
private static String utilTime(int value) {
if (value < 10)
return "0" + String.valueOf(value);
else
return String.valueOf(value);
}
// Used to convert 24hr format to 12hr format with AM/PM values
private void updateTime(int hours, int mins) {
String timeSet = "";
if (hours > 12) {
hours -= 12;
timeSet = "PM";
} else if (hours == 0) {
hours += 12;
timeSet = "AM";
} else if (hours == 12)
timeSet = "PM";
else
timeSet = "AM";
String minutes = "";
if (mins < 10)
minutes = "0" + mins;
else
minutes = String.valueOf(mins);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hours).append(':')
.append(minutes).append(" ").append(timeSet).toString();
mtxtTime.setText(aTime);
}
public void insertWork() {
String strWorktitle = medWorkTitle.getText().toString().trim();
String strWorkDescription = medWorkDescription.getText().toString().trim();
String strWorkDate = mtxtDate.getText().toString().trim();
String strWorkTime = mtxtTime.getText().toString().trim();
String strWorkValue = getWorkValue();
long id = dbHelper.insertWork(strWorktitle, strWorkDescription, strWorkDate, strWorkTime, strWorkValue, "Incomplete");
if (id != -1) {
Toast.makeText(HomeActivity.this, "Remider Added", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(HomeActivity.this, "Reminder can not be Saved", Toast.LENGTH_LONG).show();
}
}
public String getWorkValue() {
// get selected radio button from radioGroup
int selectedId = mrgWorkValue.getCheckedRadioButtonId();
// find the radiobutton by returned id
mrbWorkValue = (RadioButton) findViewById(selectedId);
return mrbWorkValue.getText().toString().trim();
}
public void generateNotification() {
Intent intent = new Intent(HomeActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(HomeActivity.this, 0,
intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(alarmManager.RTC, mCalendar.getTimeInMillis(),
pendingIntent);
Log.d("MCALENDER VALUE : ", "" + mCalendar.getTimeInMillis());
Date date2 = new Date(mCalendar.getTimeInMillis());
Toast.makeText(HomeActivity.this,"Generate Notification Called",Toast.LENGTH_LONG).show();
Toast.makeText(HomeActivity.this,"gnTime : "+ date2,Toast.LENGTH_LONG).show();
}
public void initCalender() {
long time = System.currentTimeMillis();
Date date2 = new Date(time);
Log.d("+++Current DateTime : ", "" + date2);
Toast.makeText(HomeActivity.this,"Current DateTime: "+date2,Toast.LENGTH_LONG).show();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date;
for (int i = 0; i < strDataList.size(); i++) {
try {
System.out.print("+++" + strDataList.get(i).toString());
date = formatter.parse(strDataList.get(i).toString());
if (date2 == date) {
mCalendar.set(mCalendar.MONTH, date2.getMonth());
mCalendar.set(mCalendar.DAY_OF_MONTH, date2.getDay());
mCalendar.set(mCalendar.YEAR, date2.getYear());
mCalendar.set(mCalendar.HOUR_OF_DAY, date2.getHours());
mCalendar.set(mCalendar.MINUTE, date2.getMinutes());
mCalendar.set(mCalendar.SECOND, date2.getSeconds());
Toast.makeText(HomeActivity.this,"Dates Matched "+date2,Toast.LENGTH_LONG).show();
generateNotification();
}
Log.d("+++DateFromDatabase : ", "" + date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
请让我走正确的路。谢谢。