我有在指定时间发送短信的服务,但我注意到同一条消息被多次接收。不知道为什么会这样。
在设置发送短信的时间后11:45 pm
,我在11:45 pm
, 05:58 am
,09:58 am
和收到了 4 次相同的信息01:01 pm
这是我发送短信的代码:
Intent myIntent = new Intent(AlarmActivity.this,
SendSmsService.class);
pendingIntent = PendingIntent.getService(AlarmActivity.this, 0,
myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, TimePreference.getHour(sendTime));
cal.set(Calendar.MINUTE, TimePreference.getMinute(sendTime));
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
pendingIntent);
这里是SendSmsService.java
public class SendSmsService extends Service
{
private static final String TAG = SendSmsService.class.getSimpleName();
String CompleteAddr, eContact1, eContact2, eContact3;
String smsString;
boolean acknowledged;
@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.v(TAG, "onStartCommand");
getSharedSettings();
if (acknowledged == false) {
if (eContact1 != "")
sendSMS(eContact1, smsString + CompleteAddr);
if (eContact2 != "")
sendSMS(eContact2, smsString + CompleteAddr);
if (eContact3 != "")
sendSMS(eContact3, smsString + CompleteAddr);
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
任何建议和想法将不胜感激
谢谢