ユーザー入力と表示通知に基づいてアラームを設定したい..アラームが機能していないため、何が問題なのかわかりません。
以下は私のコードです:
これは私のアラームが設定されている場所です
public class AppointmentAdd extends Activity{
private SQLiteDatabase database;
private DBHelper helper;
private Spinner spinner1;
Button btnSave, btnDate, btnTime, btnCancel;
EditText addDoctor;
DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
Calendar myCalendar = Calendar.getInstance();
SimpleDateFormat mSDF = new SimpleDateFormat("hh:mm a");
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabelDate(year, monthOfYear, dayOfMonth);
}
};
TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
myCalendar.set(Calendar.MINUTE, minute);
updateLabelTime(hourOfDay, minute);
}
};
private void updateLabelDate(int year, int monthOfYear,
int dayOfMonth) {
year = myCalendar.get(Calendar.YEAR);
monthOfYear = myCalendar.get(Calendar.MONTH);
dayOfMonth = myCalendar.get(Calendar.DATE);
btnDate.setText(new StringBuilder().append(dayOfMonth).append(".")
.append(monthOfYear + 1).append(".").append(year).append(" "));
}
private void updateLabelTime(int hourOfDay, int minute) {
hourOfDay = myCalendar.get(Calendar.HOUR_OF_DAY);
minute = myCalendar.get(Calendar.MINUTE);
String time = mSDF.format(myCalendar.getTime());
btnTime.setText(new StringBuilder().append(time));
}
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.add_app);
btnSave = (Button)findViewById(R.id.btn_save);
btnCancel = (Button)findViewById(R.id.btn_cancel);
btnDate = (Button)findViewById(R.id.btn_date);
btnTime = (Button)findViewById(R.id.btn_time);
spinner1 = (Spinner)findViewById(R.id.spin_event);
addDoctor = (EditText)findViewById(R.id.edit_doctor);
btnDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new DatePickerDialog(AppointmentAdd.this, d, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
btnTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new TimePickerDialog(AppointmentAdd.this, t, myCalendar
.get(Calendar.HOUR_OF_DAY), myCalendar
.get(Calendar.MINUTE), true).show();
}
});
helper = new DBHelper(this);
database = helper.getWritableDatabase();
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String list = spinner1.getSelectedItem().toString();
spinner1.setTag(list);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String purpose = spinner1.getTag().toString();
String date = btnDate.getText().toString();
String time = btnTime.getText().toString();
String doctor = addDoctor.getText().toString();
helper.insertDataAppointment(database, date, time, purpose, doctor);
Toast.makeText(AppointmentAdd.this, "Successfully Add", Toast.LENGTH_SHORT).show();
setAlarm(myCalendar.get(Calendar.HOUR_OF_DAY), myCalendar.get(Calendar.MINUTE),
myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
Intent i = new Intent(AppointmentAdd.this, Appointment.class);
startActivity(i);
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity (new Intent(getApplicationContext(), Appointment.class));
}
});
updateLabelDate(0, 0, 0);
updateLabelTime(0, 0);
}
private void setAlarm(int Hour, int Minute, int year, int monthOfYear, int dayOfMonth){
String name = addDoctor.getText().toString();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
cal.set(Calendar.HOUR_OF_DAY, Hour);
cal.set(Calendar.MINUTE, Minute);
String time = mSDF.format(cal.getTime());
//in case of choosing a previous hour, then set alarm to next day
if (cal.getTimeInMillis() < System.currentTimeMillis())
cal.set(year, monthOfYear, dayOfMonth, Hour + 24, Minute);
Intent intent = new Intent(this, Notify.class);
intent.putExtra("name",name);
intent.putExtra("hour", Hour);
intent.putExtra("minute", Minute);
final int _id = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(),
_id, intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()-(3*60*60*1000),pendingIntent );
Toast.makeText(this, "Notification set for: "+ dayOfMonth +"/"+ (monthOfYear+1) +"/"+ year
+ "," + time, Toast.LENGTH_SHORT).show();
}
}
これは私の通知クラスです
public class Notify extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Hello it is time for your appoinment";
long when = System.currentTimeMillis();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
CharSequence contentTitle = "My notification";
CharSequence contentText = "Appointment with Dr " + intent.getStringExtra("name");
final int NOTIF_ID = 1234;
NotificationManager notofManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, Appointment.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,1234, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText,when );
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notofManager.notify(NOTIF_ID,notification);
}
}
私を助けてください。実行すると、インテントを呼び出したときにクラスに通知されないようです