Android 启动BroadcastReceiver时从发送SMS消息
我试图在广播接收器被触发时向联系人发送SMS消息,但发送SMS消息的方法似乎在广播接收器中不起作用(在活动中工作正常)-仅向第一个联系人发送SMS,然后停止。有人能检查我的代码并告诉我问题出在哪里吗 注意:我在LogCat中没有收到任何错误或警告。我在被调用的接收端方法中调用它(检查是否有效),它只向第一个联系人发送短信,而不向其他联系人发送短信 这是我的密码:Android 启动BroadcastReceiver时从发送SMS消息,android,android-broadcast,smsmanager,Android,Android Broadcast,Smsmanager,我试图在广播接收器被触发时向联系人发送SMS消息,但发送SMS消息的方法似乎在广播接收器中不起作用(在活动中工作正常)-仅向第一个联系人发送SMS,然后停止。有人能检查我的代码并告诉我问题出在哪里吗 注意:我在LogCat中没有收到任何错误或警告。我在被调用的接收端方法中调用它(检查是否有效),它只向第一个联系人发送短信,而不向其他联系人发送短信 这是我的密码: private void sendSMS(String message) { ArrayList<Contact
private void sendSMS(String message) {
ArrayList<Contact> contacts = getContacts();
Log.d(TAG, "sengind SMS message, contacts size: " + contacts.size());
for (Contact contact : contacts) {
try {
sendSMSmsg(contact.getNumber(), message);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void sendSMSmsg(String phoneNumber, String message) {
v.vibrate(50);
ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,
new Intent(context, SmsSentReceiver.class), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,
new Intent(context, SmsDeliveredReceiver.class), 0);
try {
SmsManager sms = SmsManager.getDefault();
ArrayList<String> mSMSMessage = sms.divideMessage(message);
for (int i = 0; i < mSMSMessage.size(); i++) {
sentPendingIntents.add(i, sentPI);
deliveredPendingIntents.add(i, deliveredPI);
}
sms.sendMultipartTextMessage(phoneNumber, null, mSMSMessage,
sentPendingIntents, deliveredPendingIntents);
} catch (Exception e) {
Log.e(TAG, "sending sms ERROR");
e.printStackTrace();
}
}
private void发送短信(字符串消息){
ArrayList contacts=getContacts();
Log.d(标记“sengind SMS消息,联系人大小:”+contacts.size());
用于(联系人:联系人){
试一试{
sendsmssg(contact.getNumber(),message);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
公共同步void sendsmssg(字符串电话号码、字符串消息){
v、 振动(50);
ArrayList SentPendingEvents=新的ArrayList();
ArrayList DeliveredPendingEvents=新ArrayList();
PendingEvent sentPI=PendingEvent.getBroadcast(上下文,0,
新意图(上下文,SmsSentReceiver.class),0;
PendingEvent deliveredPI=PendingEvent.getBroadcast(上下文,0,
新意图(上下文,SmsDeliveredReceiver.class),0;
试一试{
SmsManager sms=SmsManager.getDefault();
ArrayList mSMSMessage=sms.divideMessage(消息);
对于(int i=0;i
这是广播侦听器的常见行为。您不应将其用于此类任务。您最好创建一个服务或IntentService,并从您的广播侦听器在其中启动SMS发送作业。这样应该行得通