我正在开发我的第一个Android应用程序而且我已经达到了一个突破... 所以......我在这个函数的帮助下发送短信:
void sendSMS(String phoneNumber, String message,int id, String sdeviceid) {
String SENT = "SMS_SENT"+id;
String DELIVERED = "SMS_DELIVERED"+id;
PendingIntent sentPI = PendingIntent.getBroadcast(this, id, new Intent(SENT), PendingIntent.FLAG_ONE_SHOT);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, id, new Intent(DELIVERED), PendingIntent.FLAG_ONE_SHOT);
//---when the SMS has been sent---
registerReceiver(new SentBroadCastReciever(phoneNumber, message, id, sdeviceid), new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new DeliveredBroadCastReciever(phoneNumber, message, id, sdeviceid), new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
到目前为止一切都那么好......问题是如果我重新启动手机,剩下的PendingIntents会被销毁,我不会收到短信的SENT和DELIVERED广播。
我正在考虑onStartCommand重新注册PendingIntents for SMS'es,我没有得到答案,使用此代码,每条短信:
String SENT = "SMS_SENT"+id;
String DELIVERED = "SMS_DELIVERED"+id;
PendingIntent sentPI = PendingIntent.getBroadcast(this, id, new Intent(SENT), PendingIntent.FLAG_ONE_SHOT);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, id, new Intent(DELIVERED), PendingIntent.FLAG_ONE_SHOT);
//---when the SMS has been sent---
registerReceiver(new SentBroadCastReciever(phoneNumber, message, id, sdeviceid), new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new DeliveredBroadCastReciever(phoneNumber, message, id, sdeviceid), new IntentFilter(DELIVERED));
这会有用吗?如果在重新启动电话之后和我的服务开始之前收到广播怎么办(这可能吗?我的服务开始后我还会收到广播吗?)?有没有办法清理X小时后没有触发的pendingintents?