0
안녕하세요 여러분 모두 Stackoverflow에 있습니다.Android SMS - Button.click이 두 번 발생한 후 메시지를 보냅니다.
나는이 엄청난 성가신 문제를 가지고 있습니다. AlarmManager를 사용하여 특정 시간에 SMS를 보내는 SMS 응용 프로그램을 작성하려고합니다. 그리고 제가 두 번을 버튼을 클릭 한 후이 만가 SMS를 보낼 수 있다는 것입니다 충분히 재미 특정 시간에 SMS를 보내는 완벽하게 작동합니다 (이 어려운 것 같았다 원인), 하지만 스릴 (또는 사망)?!
아무도 나는 곧 내 마음에서 나간다는 생각이 들었다. 몇 시간 동안 노력했다. MyAlarmService 클래스 여기
public void onClick(View arg0)
{
// TODO Auto-generated method stub
//txtClear();
try {
Intent myIntent = new Intent(SMSAlarm.this, MyAlarmService.class);
smsNumber = txtPhoneNo.getText().toString();
smsText = txtMessage.getText().toString();
// if(smsNumber.length()<=0 || smsText.length()<=0)
// {
// Toast.makeText(getBaseContext(),
// "Please enter both phone number and message",
// Toast.LENGTH_SHORT).show();
// }
// else
// {
TimePicker timePickerAlarm = (TimePicker) findViewById(R.id.timePickerAlarm);
DatePicker datePick = (DatePicker) findViewById(R.id.datePickerAlarm);
Calendar calendar = calendarInformation(timePickerAlarm,
datePick);
Calendar today = Calendar.getInstance();
//Check if chosen date is less than the current date and time
//IF True, inform the user that the selected date has passed
//ELSE, start the service for sending SMS
// if(calendar.getTime().getTime() <= today.getTime().getTime())
// {
// Toast.makeText(getBaseContext(), "Date has passed!\nPlease choose a new date", Toast.LENGTH_LONG).show();
// }
// else
// {
Toast.makeText(SMSAlarm.this,
"Start Alarm with \n" +
"smsNumber = " + smsNumber + "\n" +
"smsText = " + smsText,
Toast.LENGTH_LONG).show();
Bundle bundle = new Bundle();
bundle.putCharSequence("extraSmsNumber", smsNumber);
bundle.putCharSequence("extraSmsText", smsText);
myIntent.putExtras(bundle);
requestCode+= 1;
//Temporary solution. Added to make the requestCode unique, to be able to send more than one SMS in a row with different text and to a different number.
pendingIntent = PendingIntent.getService(SMSAlarm.this, requestCode, myIntent, PendingIntent.FLAG_ONE_SHOT); //PendingIntent.FLAG_ONE_SHOT
//pendingIntent = PendingIntent.getService(SMSAlarm.this, 0, myIntent, 0);
//}
//}
txtClear();
}
catch (Exception e)
{
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), "Error sending SMS " + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
txtClear();
}
}
그리고있다 :
어떤 도움이 많이 감사합니다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 IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}
@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)
{
//TODO Auto-generated method stub
try {
super.onStart(intent, startId);
Bundle bundle = intent.getExtras();
smsNumberToSend = (String) bundle.getCharSequence("extraSmsNumber");
smsTextToSend = (String) bundle.getCharSequence("extraSmsText");
Toast.makeText(this, "Sending Message...", Toast.LENGTH_LONG).show();
Toast.makeText(this,
"Number = " + smsNumberToSend + "\n" +
"Message = " + smsTextToSend,
Toast.LENGTH_LONG).show();
sendSMS(smsNumberToSend, smsTextToSend);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendSMS(String phoneNumber, String message) {
// TODO Auto-generated method stub
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
}
@Override
public boolean onUnbind(Intent intent)
{
//TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
}
언제 MyAlarmService가 시작 되었습니까? – Peter
pendingIntent = PendingIntent.getService (SMSAlarm.this, requestCode, myIntent, PendingIntent.FLAG_ONE_SHOT); Button.click 이벤트에서 발생하지만 Button : S를 두 번째로 클릭했을 때만 발생합니다. – Anton