Android消息传递

时间:2014-08-06 11:51:45

标签: android

我的应用程序每10分钟向不同的号码发送不同的消息...如果消息传递它不能再发送消息,否则再次发送...我的问题在于交付部分,它是如何理解哪些SMS发送的。 ..

MessageID对于每封邮件都是唯一的,我试图使用它,但它没有用......

这是我的sendSMS方法:

private void sendSMS(String phoneNumber, String message, final int MessageID)
    {      
        final String SMS_SEND_ACTION = "CTS_SMS_SEND_ACTION";
        final String SMS_DELIVERY_ACTION = "CTS_SMS_DELIVERY_ACTION";

        //---when SMS send---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                    {
                        //Toast.makeText(getBaseContext(), "پیغام ارسال شد.",Toast.LENGTH_SHORT).show();
                       // DataAccess.UpdateTblMessageSend (getBaseContext(), MessageID ,1);
                        break;
                    }
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }   
            }
        }, new IntentFilter(SMS_SEND_ACTION)); //SENT

        //---when SMS delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                    { 
                        //Toast.makeText(getBaseContext(), "پیغام دلیور شد.", Toast.LENGTH_SHORT).show();
                        Log.i("DELIVERED:", "Context: " + arg0 + "--- ID:  " + MessageID); 
                        DataAccess.UpdateTblMessageSend (getBaseContext(), MessageID, 2);
                        break;
                    }
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;                  
                }
            }

        }, new IntentFilter(SMS_DELIVERY_ACTION)); //DELIVERED

        SmsManager sms = SmsManager.getDefault();
        ArrayList<String> parts =sms.divideMessage(message);

        ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
        ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>(); 

        for (int i = 0; i < parts.size(); i++)
        {
            sentIntents.add(PendingIntent.getBroadcast(this, 0, new Intent(SMS_SEND_ACTION), 0));
            deliveryIntents.add(PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERY_ACTION), 0));
        }
        sms.sendMultipartTextMessage(phoneNumber,null, parts, sentIntents, deliveryIntents);              
    }

1 个答案:

答案 0 :(得分:0)

public static String ACTION_SMS_SENT = "SMS_SENT";
public static String ACTION_SMS_DELIVERED = "SMS_DELIVERED";
public static final String ExtraStamp = "stamp";
public static final String ExtraNum = "num";
public static final String ExtraOPN = "opn";
public static final String ExtraPN = "pn";
public static final String ExtraName = "name";

    Intent sendIntent = new Intent(ACTION_SMS_SENT + num);
    sendIntent.putExtra(ExtraStamp, stamp);
    sendIntent.putExtra(ExtraNum, num);
    sendIntent.putExtra(ExtraPN, pn);
    sendIntent.putExtra(ExtraOPN, opn);
    sendIntent.putExtra(ExtraName, name); 

    Intent DeliveredIntent = new Intent(ACTION_SMS_DELIVERED + num);
    DeliveredIntent.putExtra(ExtraStamp, stamp);
    DeliveredIntent.putExtra(ExtraNum, num);
    DeliveredIntent.putExtra(ExtraPN, pn);
    DeliveredIntent.putExtra(ExtraOPN, opn);
    DeliveredIntent.putExtra(ExtraName, name);



    PendingIntent piSent = PendingIntent.getBroadcast(c, 0, sendIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent piDelivered = PendingIntent.getBroadcast(c, 0,
            DeliveredIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    SmsManager smsManager = SmsManager.getDefault();

 smsManager.sendTextMessage(pn, null, message, piSent, piDelivered);

和接收器

    String action = intent.getAction();
    Bundle extra = intent.getExtras();
    String stamp = (String) extra.get(send_sms.ExtraStamp);
    int num = extra.getInt(send_sms.ExtraNum);
    String opn = extra.getString(send_sms.ExtraOPN);
    String pn = extra.getString(send_sms.ExtraPN);
    String name = extra.getString(send_sms.ExtraName);
    ContactSendDBHandler db = new ContactSendDBHandler(context);

    if (action.equals(send_sms.ACTION_SMS_SENT + num)) 
        switch (getResultCode()) {
            case Activity.RESULT_OK:

          }