我正在尝试在我的应用程序中安排短信。我正在通过 TimePicker 设置时间和通过 DatePicker 设置日期。但不知何故,代码不起作用。请告诉我哪里出错了。吨
主要代码:
int Hour = Time_Picker.getCurrentHour();
int Minute = Time_Picker.getCurrentMinute();
DatePicker Date_Picker = (DatePicker)findViewById(R.id.datePicker1);
int day = Date_Picker.getDayOfMonth();
int month = Date_Picker.getMonth() + 1;
int year = Date_Picker.getYear();
Intent myIntent = new Intent(ScheduleMessage.this, MyAlarmService.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("Number", Number.getText().toString());
bundle.putCharSequence("Message", Message.getText().toString());
myIntent.putExtras(bundle);
pendingIntent = PendingIntent.getService(ScheduleMessage.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//calendar.add(Calendar.SECOND, 10);
calendar.set(year, month, day, Hour, Minute);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(ScheduleMessage.this,"Start Alarm with \n" +
"smsNumber = " + Number.getText().toString() +
"\n" + "smsText = " + Message.getText().toString() + "\nScheduled for :"
+ Hour +" "+Minute,
Toast.LENGTH_LONG).show();
`
这是服务:
public class MyAlarmService extends Service {
String smsNumberToSend, smsTextToSend;
@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
//super.onStart(intent, startId);
Bundle bundle = intent.getExtras();
smsNumberToSend = (String) bundle.getCharSequence("Number");
smsTextToSend = (String) bundle.getCharSequence("Message");
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
Toast.makeText(this,
"MyAlarmService.onStart() with \n" +
"smsNumberToSend = " + smsNumberToSend + "\n" +
"smsTextToSend = " + smsTextToSend,
Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(smsNumberToSend, null, smsTextToSend, null, null);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
有趣的是,当我添加该行时calendar.set(Calendar.SECOND,10);
- 一切正常。但是当我使用相同的方法并设置特定的时间时,什么也没有发生。
请帮助我,我是Android新手。