发送短信的独特和接收looong短信的android [英] Sending unique sms and receiving looong sms in android

查看:461
本文介绍了发送短信的独特和接收looong短信的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经尝试了很长的时间来寻找一种方法,使一个应用程序,可以在Android的发送和接收短信。这工作正常。这里是code:

So i have tried for a long time to find a way to make a app that can send and receive sms in android. That works fine. Here is the code:

有关发送:

@SuppressWarnings("deprecation")
public void sendSMS(String phoneNumber, String message) {


    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    int unq = 0;
    Intent sent = new Intent(SENT);
    sent.putExtra("unq", unq);
    Intent delivered = new Intent(DELIVERED);
    delivered.putExtra("unq", unq);

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sent, 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,delivered, 0);

    // ---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {

            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS sent",
                        Toast.LENGTH_SHORT).show();
                smsstatus = "0";
                smserror = "noError";
                //sendSmsStatus();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "Generic failure",
                        Toast.LENGTH_SHORT).show();
                setSmsstatus("1");
                setSmserror("Generic failure");
                sendSmsStatus("Generic failure");
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "No service",
                        Toast.LENGTH_SHORT).show();
                setSmsstatus("2");
                setSmserror("No service");
                sendSmsStatus("No service");
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "Null PDU",
                        Toast.LENGTH_SHORT).show();
                setSmsstatus("3");
                setSmserror("Null PDU");
                sendSmsStatus("Null PDU");
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "Radio off",
                        Toast.LENGTH_SHORT).show();
                setSmsstatus("4");
                setSmserror("Radio off");
                sendSmsStatus("Radio off");
                break;
            }

        }

    }, new IntentFilter(SENT));

    // ---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {

            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS delivered",
                        Toast.LENGTH_SHORT).show();

                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(getBaseContext(), "SMS not delivered",
                        Toast.LENGTH_SHORT).show();

                break;
            }

        }
    }, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

有关接收:

public class SmsReceiver extends BroadcastReceiver {
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
    // ---get the SMS message passed in---

    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    Object sms = "";
    ArrayList<String> s = new ArrayList<String>();
    Manager m = Factory.getInstance().getManager();

    if (bundle != null) {
        // ---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            str += "    SMS fra " + msgs[i].getOriginatingAddress() + "\n";
            str += "    Besked: ";
            str += msgs[i].getMessageBody().toString();
            str += "\n";
            sms = "SMS = "+msgs[i].getOriginatingAddress()+","+msgs[i].getMessageBody().toString();
            s.add(msgs[i].getOriginatingAddress());
            s.add(msgs[i].getMessageBody().toString());
        }
        // ---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_LONG).show();
        Manager.toastIt(context, str);              

        // Send the sms to the server
        //Connection.send(new Transmit("SmsReceived",s));
    }
}
}

这个伟大的工程!

在这里,问题来了。它是更多钞票来refactore我的code才达到以下内容:

And here comes the question. Is it posible to refactore my code to achive the following:

  1. 对我发送的短信独特的indentifier /标志,以便我可以确保该短信给我接收状态。正如你可以看到我已经试图把多余的我2意图,也许这是正确的方式,但不是唯一这样做我不是现在如何检查/接收/提取标志的地位,同时也标志真的独特的现在。

  1. Have a unique indentifier/flag on my send sms so i can make sure which sms i receive a status on. As you can see i have already tried to put extra on my 2 intents, and maybe that is the right way, but not only do i not now how to check/receive/extract the flag for the status, but also the flag really "unique" right now.

其漂亮,我可以revieve短信,但是当其超过160个字符只显示我的第160个字符。我已经看到了如何GTalkSMS做的,但希望我的code可能只是重构了一下:)

Its nice that I can revieve a sms, but when its more than 160 chars it only shows me the first 160 chars. I had looked at how GTalkSMS does it, but was hoping my code could just be refactored a bit :)

最后一个问题是,上述的2的混合。我不能发送一条短信,多数民众赞成超过160字符。我知道我必须使用sendMultipartTextMessage,但我不知道怎么办。我的想法是,我可以用100 devide字符串消息到数组,但我不知道。

The last problem is, a mix of the 2 above. I cant send a sms thats more than 160 char. I know i have to use sendMultipartTextMessage, but i dont know how. My thought is that i could devide the "String message" by 100 to a Array but i dont know.

可以随意refactore的code。我很期待看到你的回复! :D 请询问你需要什么解释更好或更code! :)

So feel free to refactore the code. I'm looking forward to see your replies! :D Please ask if you need anything explained better or more code! :)

推荐答案

不是Android的特异性,但阅读了连锁短信的标准。基本上,这是每一个指定他们去的previous 1多封邮件,但它会在空气中完全独立的短信。

Not Android specific, but read about the "Concatenated SMS" standard. Basically it's multiple messages that each specify they go with the previous one, but it goes over the air as entirely independent SMS.

大多数手机隐瞒这一事实从用户,当然,但如果你直接接收SMS很可能你需要处理它自己 - 发送的的接收。假设Android使用这个标准,这似乎是一个安全的赌注。

Most phones hide this fact from the user, of course, but if you're directly receiving SMS it's likely you'll need to deal with it yourself - sending and receiving. Assuming Android uses this standard, which seems like a safe bet.

由于它是如此普遍,我相信你能找到一个库,有人在已经写好的。

Since it's so common, I'm sure you can find a library that somebody's already written.

http://en.wikipedia.org/wiki/Concatenated_SMS

这篇关于发送短信的独特和接收looong短信的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆