我的 onReceive 响应哪个请求?
我有一个联系人数组列表,其中包含电话号码和状态字段。我想更新成功发送结果的状态字段。
我什至尝试将 ArrayList 项的索引传递到 Pending Intent 中,并使用 contacts.indexOf(c.getPhoneNumber()) 作为 int requestCode。我只是不知道如何判断哪条消息报告成功并更新我的联系人项目。
package com.example.SMS;
public class SendSMSActivity extends SMSActivity {
private EditText smsMessageBody;
private Button send_button;
private SmsManager sms;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sendsmsactivity);
setupViews();
}
private void setupViews() {
smsMessageBody = (EditText) findViewById(R.id.smsText_editText1);
broadcast_button = (Button) findViewById(R.id.sendsms_button);
send_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (smsMessageBody.getText().toString().length() > 0 ) {
if (!getSendSMSApplication().getCurrentContacts().isEmpty()) {
sendSMSLoop();
Toast.makeText(getBaseContext(), R.string.messages_sent, Toast.LENGTH_SHORT).show();
getSendSMSApplication().setCurrentContacts(null); // Clear existing contacts after send
finish();
} else {
Toast.makeText(getBaseContext(), R.string.error_no_numbers, Toast.LENGTH_SHORT).show();
} // there are phone numbers to send to?
} else {
Toast.makeText(getBaseContext(), R.string.error_no_message, Toast.LENGTH_SHORT).show();
} // user entered a message?
}
});
}
protected void sendSMSLoop() {
ArrayList<ContactItem> contacts = getSendSMSApplication().getCurrentContacts();
for (ContactItem c:contacts) {
sendSMS(c.getPhoneNumber().toString(), smsMessageBody.getText().toString(), contacts.indexOf(c.getPhoneNumber()));
}
}
public void sendSMS(String phoneNumber, String message, int messageIndex)
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(this, messageIndex, new Intent(SENT), 0);
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), R.string.sms_sent, Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, null);
}
}
I have an ArrayList of contacts that contains a phone number and a status field. I want to update the status field on a successful send result.
I even tried passing the index of the ArrayList item in to the Pending Intent with contacts.indexOf(c.getPhoneNumber()) as the int requestCode. I just cant' figure out how to tell which message is reporting back as successful and update my contact item.
package com.example.SMS;
public class SendSMSActivity extends SMSActivity {
private EditText smsMessageBody;
private Button send_button;
private SmsManager sms;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sendsmsactivity);
setupViews();
}
private void setupViews() {
smsMessageBody = (EditText) findViewById(R.id.smsText_editText1);
broadcast_button = (Button) findViewById(R.id.sendsms_button);
send_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (smsMessageBody.getText().toString().length() > 0 ) {
if (!getSendSMSApplication().getCurrentContacts().isEmpty()) {
sendSMSLoop();
Toast.makeText(getBaseContext(), R.string.messages_sent, Toast.LENGTH_SHORT).show();
getSendSMSApplication().setCurrentContacts(null); // Clear existing contacts after send
finish();
} else {
Toast.makeText(getBaseContext(), R.string.error_no_numbers, Toast.LENGTH_SHORT).show();
} // there are phone numbers to send to?
} else {
Toast.makeText(getBaseContext(), R.string.error_no_message, Toast.LENGTH_SHORT).show();
} // user entered a message?
}
});
}
protected void sendSMSLoop() {
ArrayList<ContactItem> contacts = getSendSMSApplication().getCurrentContacts();
for (ContactItem c:contacts) {
sendSMS(c.getPhoneNumber().toString(), smsMessageBody.getText().toString(), contacts.indexOf(c.getPhoneNumber()));
}
}
public void sendSMS(String phoneNumber, String message, int messageIndex)
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(this, messageIndex, new Intent(SENT), 0);
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), R.string.sms_sent, Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, null);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我现在正在尝试自己做这件事。我猜您遇到了和我一样的问题,如果您发送多于 1 条消息,它们都会为所有 3 条消息返回相同的发送代码。我认为这里的技巧是“线程”每条消息,以便每个“线程”都有不同的返回代码。我不能保证这就是解决方案,但这是我下一步要解决这个问题的路径。
希望这对您有所帮助,如果您解决了,也请告诉我。
I am trying to do this myself at the moment. I guess you have got the same problem as me and if you are sending more than 1 message, they are all returning the same sent code for all 3 messages. I think the trick here is to 'thread' each message so that each 'thread' will have a different return code. I can't guarantee this is the solution but it is the path I am next going to go down for solving this issue.
Hope this helps, if you work it out please let me know too.
这是取自我的实际代码的一些代码:
尝试将接收/传递的意图传递给此函数(完成该函数以添加您需要的功能),我将在几周内尝试它,所以让我知道它是如何进行的,如果你比我先一步。
只是重新查看了您的代码...
我的代码看起来更像是:
据我所知,您需要添加额外的意图来使用 pdu。
This is some code taken from my actual code:
Try passing the received/delivered intent to this function (complete the function to add the functionality you need), I will be trying it in a few weeks time so let me know how it goes if you beat me to it.
Just re-looked over your code...
My code looks more like:
You need to add the extra intent to work with the pdus, as far as I am aware.