第一个问题:我试图获取当前时间以设置时间选择器对话框的默认时间,但它没有给出正确的时间,我会粘贴代码
第二个问题:警报立即触发,可能是什么问题?有一个文本视图和三个按钮。 文本视图显示了这个说法。 第一个按钮显示timepicker对话框。 第二个按钮,用于更改textview文本。 第三个按钮,使用先前使用第一个按钮的时间选择器对话框保存在变量中的时间来设置闹钟。
public class MainActivity extends Activity implements View.OnClickListener{
// declare our view variables
private TextView mSayingTextView;
private TextView mMainTextView;
private Button mNextButton;
private Button mAlarmButton;
private Button mSetTimeButton;
static final int TIME_DIALOG_ID=0;
public int hour,minute;//users` selection
private int mHour,mMinute;//first values appear
long timeInMillis;
public MainActivity(){
//assign current time values
final Calendar c= Calendar.getInstance();
mHour=c.get(Calendar.HOUR_OF_DAY);
mMinute=c.get(Calendar.MINUTE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Assign the views from the layout file to the corresponding variables
mSayingTextView = (TextView)findViewById(R.id.mainTextView);
mMainTextView=(TextView)findViewById(R.id.abbTextView);
mNextButton= (Button) findViewById(R.id.nxtButton);
mNextButton.setOnClickListener(this);
mAlarmButton=(Button)findViewById(R.id.alarmButton);
mAlarmButton.setOnClickListener(this);
mSetTimeButton =(Button)findViewById(R.id.intentBtn);
mSetTimeButton.setOnClickListener(this);
}
//Register time picker dialog listener
private TimePickerDialog.OnTimeSetListener mTimeSetListener=new TimePickerDialog.OnTimeSetListener(){
public void onTimeSet(TimePicker view, int hourOfDay, int min){
hour=hourOfDay;
minute=min;
timeInMillis=hour*60*60*1000 + minute*60*1000;
mSetTimeButton.setText("Time now :"+mHour+":"+mMinute);
}
};
@Override
protected Dialog onCreateDialog(int id){
return new TimePickerDialog(this,mTimeSetListener,mHour,mMinute,false);
}
public void scheduleAlarm(){
// Long time=new GregorianCalendar().getTimeInMillis()+1*60*1000;
Long time = timeInMillis;
Intent intentAlarm = new Intent(this,AlarmReceiver.class);
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1,intentAlarm,PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(this,"Alarm scheduled",Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.nxtButton:
// do your code
String[] sayings = {
"saying1" ,
"saying2"
,
"saying3"
};
String saying="";
Random randomGenerator = new Random();
int randomNumber=randomGenerator.nextInt(sayings.length);
saying=sayings[randomNumber];
mSayingTextView.setText(saying);
break;
case R.id.alarmButton:
scheduleAlarm();
break;
case R.id.intentBtn:
showDialog(TIME_DIALOG_ID);
break;
default:
break;
}
}
}
报警接收器中的代码:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int duration=Toast.LENGTH_SHORT;
Toast.makeText(context,"alarm set",duration).show();
}
}
答案 0 :(得分:0)
您正在创建过去时间的闹钟。然后系统立即触发警报。您的timeInMillis
参数错误。你只用毫秒设置小时和分钟。您还应该设置日,月,年。将onTimeSet
TimePickerDialog
函数更改为:
public void onTimeSet(TimePicker view, int hourOfDay, int min){
hour=hourOfDay;
minute=min;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
timeInMillis = calendar.getTimeInMillis();
mSetTimeButton.setText("Time now :"+mHour+":"+mMinute);
}
我使用Calendar
将日期,月份和年份设置为timeInMillis
。
此外,您的TimePickerDialog
未设置为当前时间。由于覆盖方法mHour
,未设置mMinute
和protected Dialog onCreateDialog(int id)
个参数。您应该编写一个可以在mHour
和mMinute
设置后调用的函数。这就是你如何做到的:
private void showDialog(){
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false).show();
}
请调用此方法而不是showDialog(TIME_DIALOG_ID)
:
...
case R.id.intentBtn:
showDialog();
break;
...
祝你好运。