|
我必须执行一个计划短信程序,除了唯一的一个以外,其他所有内容都可以正常运行:第二次尝试发送短信时,该程序将与第一个相同的内容发送到与第一个相同的电话号码中第一次。
这是我的代码:
main_activity,sendBtn:
sendBtn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
Intent myIntent = new Intent(main_activity.this, MyAlarmService.class);
String smsNumber = phoneNo.getText().toString();
String smsText = edit.getText().toString();
if (smsNumber.length() != 0 && smsText.length() != 0){
Bundle bundle = new Bundle();
bundle.putCharSequence(\"extraSmsNumber\", smsNumber);
bundle.putCharSequence(\"extraSmsText\", smsText);
myIntent.putExtras(bundle);
String text = mTimeDisplay.getText().toString() + \" \" + mDateDisplay.getText().toString();
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat(\"HH:mm dd/MM/yyyy\");
date = (Date)formatter.parse(text);
Calendar setCalendar = Calendar.getInstance();
setCalendar.setTime(date);
pendingIntent = PendingIntent.getService(main_activity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.set(AlarmManager.RTC_WAKEUP, setCalendar.getTimeInMillis(), pendingIntent);
Toast.makeText(main_activity.this,
\"Start Alarm with \\n\" +
\"smsNumber = \" + smsNumber + \"\\n\" +
\"smsText = \" + smsText + \"\\n Contain : \" + text,
Toast.LENGTH_LONG).show();
}
else {
showDialog(ALERT_DIALOG_ID);
}
}
catch (ParseException e)
{Toast.makeText(main_activity.this,\"Error!!!\",Toast.LENGTH_LONG).show(); }
}
}
);
MyAlarmService:
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.widget.Toast;
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
super.onStart(intent, startId);
Bundle bundle = intent.getExtras();
smsNumberToSend = (String) bundle.getCharSequence(\"extraSmsNumber\");
smsTextToSend = (String) bundle.getCharSequence(\"extraSmsText\");
sendSMS(smsNumberToSend, smsTextToSend);
}
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.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);
}
}
我真的需要帮助,请帮助我尽快找到解决方案,这非常有用