我在处理程序上有这个示例应用程序,它在某个时间触发一个日志,但是当我单击我的开始按钮时它似乎不起作用。下面是我的代码。
public class Main extends Activity {
private long selectedTimeInMills;
private Handler handler;
private static final Calendar CALENDAR = Calendar.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TimePicker timepicker = (TimePicker) findViewById(R.id.timePicker1);
final TextView timeText = (TextView) findViewById(R.id.time);
Button start = (Button) findViewById(R.id.button1);
handler = new Handler();
String textTime = formatDate(CALENDAR.getTimeInMillis(), "hh:mm a");
timeText.setText(textTime);
timepicker.setOnTimeChangedListener(new OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
CALENDAR.set(Calendar.HOUR_OF_DAY, hourOfDay);
CALENDAR.set(Calendar.MINUTE, minute);
selectedTimeInMills = CALENDAR.getTimeInMillis();
String textTime = formatDate(CALENDAR.getTimeInMillis(), "hh:mm a");
timeText.setText(textTime);
}
});
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Runnable runnable = new Runnable() {
public void run() {
Log.d("HandlerCheck", "Handler Fired! @ ("+selectedTimeInMills+") "+formatDate(selectedTimeInMills, "hh:mm a"));
}
};
boolean flag = handler.postAtTime(runnable, selectedTimeInMills);
Toast.makeText(Main.this, "Handler is Fired?:: "+flag, Toast.LENGTH_LONG).show();
}
});
}
}
注意:当我点击我的按钮并实现handler.postAtTime()
它返回true
但它仍然不会在 DDMS 上显示日志,即使我已将我的时间选择器设置为距当前时间 1 分钟仍然不会在 DDMS 上触发日志消息