アプリでテキスト メッセージのスケジュールを設定しようとしています。TimePicker を介して Time を設定し、DatePicker を介して Date を設定しています。しかし、どういうわけかコードは機能しません。どこが間違っているのか教えてください。T
メインコード:
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が初めてです。