我想生成一些通知。当用户在timePicker对话框中设置时间时,将生成此通知。这是场景: 当我点击按钮时,它会显示timepicker对话框,用户可以选择时间..但是当我想设置时间时,应用程序卡住并要求强行关闭..我想要的是当用户设置时间让我们说下午10时45分,通知将于下午10时45分产生,以提醒用户。
这是我的TabTwoclass:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_two);
btnBrfkst = (Button) findViewById(R.id.button1);
btnMtea = (Button) findViewById(R.id.button2);
btnLunch = (Button) findViewById(R.id.button3);
btnAftea = (Button) findViewById(R.id.button4);
btnDinner = (Button) findViewById(R.id.button5);
btnSupper = (Button) findViewById(R.id.button6);
mCalen = Calendar.getInstance();
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
btnBrfkst.setOnClickListener(this);
btnMtea.setOnClickListener(this);
btnLunch.setOnClickListener(this);
btnAftea.setOnClickListener(this);
btnDinner.setOnClickListener(this);
btnSupper.setOnClickListener(this);
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
switch (id) {
case Time_PICKER_ID:
return new TimePickerDialog(this, TimePickerListener,
hourOfDay, minute, false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener TimePickerListener =
new TimePickerDialog.OnTimeSetListener() {
private Calendar current;
// while dialog box is closed, below method is called.
public void onTimeSet(TimePicker view, int hour, int minute) {
switch (timePickerInput) {
case R.id.button1:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr = (ampm == 0) ? "AM" : "PM";
// Set the Time String in Button
btnBrfkst.setText(hour12format + " : " + minute + " / " + ampmStr);
break;
case R.id.button2:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format2 = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr2 = (ampm == 0) ? "AM" : "PM";
btnMtea.setText(hour12format2 + " : " + minute + " / " + ampmStr2);
break;
case R.id.button3:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format3 = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr3 = (ampm == 0) ? "AM" : "PM";
btnLunch.setText(hour12format3 + " : " + minute + " / " + ampmStr3);
break;
case R.id.button4:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format4 = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr4 = (ampm == 0) ? "AM" : "PM";
btnAftea.setText(hour12format4 + " : " + minute + " / " + ampmStr4);
break;
case R.id.button5:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format5 = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr5 = (ampm == 0) ? "AM" : "PM";
btnDinner.setText(hour12format5 + " : " + minute + " / " + ampmStr5);
break;
case R.id.button6:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format6 = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr6 = (ampm == 0) ? "AM" : "PM";
btnSupper.setText(hour12format6 + " : " + minute + " / " + ampmStr6);
break;
}
if(mCalen.compareTo(current) <= 0){
//The set Date/Time already passed
Toast.makeText(getApplicationContext(),
"Invalid Date/Time",
Toast.LENGTH_LONG).show();
}else{
setAlarm(mCalen);
}
}
};
private void setAlarm(Calendar targetmCalen){
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetmCalen.getTimeInMillis(), pendingIntent);
}
@Override
public void onClick(View v) {
timePickerInput = v.getId();
showDialog(Time_PICKER_ID);
这是我的AlarmReceiver类,我在其中生成通知
private static final int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Time is set", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification = new NotificationCompat.Builder(context)
.setContentTitle("eMedicine")
.setContentText("Time to take your medicine")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
}