我正在做一个示例警报应用程序。
设置时间后,设置广播但是当时没有调用接收方法
ReminderMainActivity:
public class ReminderMainActivity extends Activity implements OnClickListener, TimePicker.OnTimeChangedListener {
DatePicker _datePicker = null;
TimePicker _timePicker = null;
EditText _taskEditText = null;
Button _setButton = null;
String _st_time = null;
String _st_date = null;
Calendar _calendar = null;
Calendar _calendarNow = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_view);
init();
}
private void init() {
// TODO Auto-generated method stub
_datePicker = (DatePicker) findViewById(R.id.date_picker);
_timePicker = (TimePicker) findViewById(R.id.time_picker);
_taskEditText = (EditText) findViewById(R.id.task_desc);
_setButton = (Button) findViewById(R.id.set_button);
_calendarNow = Calendar.getInstance();
_calendar = (Calendar) _calendarNow.clone();
_timePicker.setOnTimeChangedListener(this);
_setButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String st_taskDescription = _taskEditText.getText().toString();
int year = _datePicker.getYear();
int monthOfYear = _datePicker.getMonth();
int dayOfMonth = _datePicker.getDayOfMonth();
_calendar.set(Calendar.YEAR, year);
_calendar.set(Calendar.MONTH, monthOfYear);
_calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
_calendar.set(Calendar.SECOND, 0);
_calendar.set(Calendar.MILLISECOND, 0);
Log.i("", "Alarm Set: " + _calendar.getTimeInMillis());
if(_calendar.compareTo(_calendarNow) <= 0){
//Today Set time passed, count to tomorrow
_calendar.add(Calendar.DATE, 1);
Log.i("", "Time passed : " + _calendar.getTimeInMillis());
Toast.makeText(this, "Time passed" + _calendar.getTimeInMillis(), Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ReminderMainActivity.this, 0, intent, 0);
AlarmManager alarm_manager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm_manager.set(AlarmManager.RTC_WAKEUP, _calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(this, "Alarm Set: " + _calendar.getTimeInMillis(), Toast.LENGTH_SHORT).show();
}
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
_calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
_calendar.set(Calendar.MINUTE, minute);
_st_time = hourOfDay + " : " + minute;
}
}
接收者类
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.i("AlarmActivity", "Receiver");
Toast.makeText(arg0, "Intent Received", Toast.LENGTH_SHORT).show();
}
}
如果我用_calendar.getTimeInMillis()
替换
System.CurrentTimeInMillis()
然后它起作用..但那不是我想要的
清单中也提到了Receiver类。 我无法找到错误。有人可以帮忙吗?我错过了什么吗?
答案 0 :(得分:0)
获得解决方案。获取日历实例后,清除值。
_calendarNow = Calendar.getInstance();
_calendar = (Calendar) _calendarNow.clone();
_calendar.clear();