我正在开发多次Android Alarm应用程序,但在第一步我有问题是它没有区分AM和PM我不知道我的代码有什么问题。下面是我的MainActivity类代码我有问题我有时间从时间选择器发出警报。
Button start;
TimePicker timeP;
TextView tv;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.button1);
timeP = (TimePicker) findViewById(R.id.timePicker1);
et = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.tex);
// tv.setText(""+System.currentTimeMillis()+20000);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
String messg = et.getText().toString();
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR, timeP.getCurrentHour());
time.set(Calendar.MINUTE, timeP.getCurrentMinute());
//time.setTimeInMillis(System.currentTimeMillis());
time.set(Calendar.SECOND, 0);
time.set(Calendar.AM_PM, time.PM);
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("msg", messg);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this,
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,time.getTimeInMillis(), pi);
);}
谢谢。
答案 0 :(得分:0)
确实,使用12小时时钟,使用24小时制来实现。抓住您的TimePicker时间并解析为24小时时钟检查代码:
_Strtime="11:30 AM";
SimpleDateFormat mDateFormat1 = new SimpleDateFormat("HH:mm");
mCalendar.setTime(mDateFormat1.parse(_Strtime));
PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,mCalendar.getTimeInMillis(), pi);