我的 onReceive 响应哪个请求?

发布于 2024-10-18 09:57:43 字数 2842 浏览 8 评论 0原文

我有一个联系人数组列表,其中包含电话号码和状态字段。我想更新成功发送结果的状态字段。

我什至尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花ソ最美的依靠 2024-10-25 09:57:43

我现在正在尝试自己做这件事。我猜您遇到了和我一样的问题,如果您发送多于 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.

追星践月 2024-10-25 09:57:43

这是取自我的实际代码的一些代码:

public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
    if (bundle != null) {
        Object[] pdus = (Object[]) bundle.get("pdus");
        final SmsMessage[] messages = new SmsMessage[pdus.length];
        for (int i = 0; i < pdus.length; i++)
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
        if (messages.length > -1) {
//The 2 variables you need (from and body)
messages[0].getOriginatingAddress();
messages[0].getMessageBody();
...

尝试将接收/传递的意图传递给此函数(完成该函数以添加您需要的功能),我将在几周内尝试它,所以让我知道它是如何进行的,如果你比我先一步。

只是重新查看了您的代码...

sms.sendTextMessage(phoneNumber, null, message, sentPI, null);

我的代码看起来更像是:

sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

据我所知,您需要添加额外的意图来使用 pdu。

This is some code taken from my actual code:

public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
    if (bundle != null) {
        Object[] pdus = (Object[]) bundle.get("pdus");
        final SmsMessage[] messages = new SmsMessage[pdus.length];
        for (int i = 0; i < pdus.length; i++)
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
        if (messages.length > -1) {
//The 2 variables you need (from and body)
messages[0].getOriginatingAddress();
messages[0].getMessageBody();
...

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...

sms.sendTextMessage(phoneNumber, null, message, sentPI, null);

My code looks more like:

sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

You need to add the extra intent to work with the pdus, as far as I am aware.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文