-1

我想根据用户输入设置警报并显示通知。我不知道出了什么问题,因为我的警报不起作用。

下面是我的代码:

这是我设置闹钟的地方

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

}

请帮我。当我运行时,似乎在调用意图时,它不会去通知类

4

1 回答 1

0

首先,确保注册 BroadcastReceiver。我将在清单中将其注册为

   <application ... >
         ...
        <receiver android:name="Notify"></receiver>
    </application>

现在,请注意日历。请注意,MONTH 从零开始。因此,要在 8 月设置警报,请将 MONTH 设置为 7。我建议在将警报设置为之前包含一个日志跟踪,

    AlarmManager alarmManager =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Log.d("Nzm", "Setting alarm on:"+cal.getTime().toLocaleString());// use either Log or Toast
    alarmManager.set(AlarmManager.RTC_WAKEUP,  cal.getTimeInMillis(),pendingIntent);
于 2013-08-18T16:36:01.393 回答