本文介绍了Android的sendTextMessage将在exceution两个相同的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直工作在一个应用程序,发送短信消息。我的问题是,sendTextMessage方法发送相同内容的两条消息。我该如何解决呢?

I've been working on an app that sends SMS-messages. The problem I have is that the sendTextMessage method sends two messages with the same content. How do I fix that?

本级启动进程

public class C2DMMessageReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //Some stuff
        Log.i("C2DMMessageReceiver", "Got C2DM message");
        SmsSend message = new SmsSend(context, phonenumber, line);
        message.send()
    }
}

发送短信类

public class SmsSend {
    SmsSend(Context tcontext, String phoneNumber, String smstext){
        context = tcontext;
        phone_number = phoneNumber;
        message = smstext; 
    }

    protected void send(){

        if(foo){
            Log.i("SmsSend", "Sending message");
            SmsManager sms = SmsManager.getDefault();
            String sent = "android.telephony.SmsManager.STATUS_ON_ICC_SENT";
            PendingIntent piSent = PendingIntent.getBroadcast(context, 0, new Intent(sent), 0);
            sms.sendTextMessage(phone_number, null, message, piSent, null);
        }

    }
}

类来找出什么happining

class to find out what's happining

public class SmsSentBroadcastReciever extends BroadcastReceiver{
    private static final String TAG = "SmsSentBroadcastReciever";

    @Override
    public void onReceive(Context context, Intent intent) {
        switch (getResultCode()){
            case Activity.RESULT_OK:
                Log.i(TAG,"SMS sent");
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Log.e(TAG,"Generic failure");
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Log.e(TAG,"No service");
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Log.e(TAG,"PDU NULL");
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Log.e(TAG,"Radio off");
                break;
        }

    }

}

从LogCat中的输出是

The output from LogCat is

得到C2DM消息

发送邮件

短信发送

短信发送

所以sendTextMessage只发射一次,但它仍然抛出两个消息。怎么办?

So the sendTextMessage is only fired once but it still throws two messages. What to do?

我与调试设备是三星Galaxy S2采用Android 4.0。我读了一些旧的线程sendTextMessage是某些(HTC)破碎设备,所以我试着用sendMultipartTextMessage,但它给出了相同的结果。

The device I'm debugging with is a Samsung Galaxy S2 with Android 4.0. I read some old threads that sendTextMessage is broken on some (HTC) devices so I tried with sendMultipartTextMessage but it gives the same result.

推荐答案

随着code正常工作,S2与ICS:

Following code works fine, S2 with ICS:

void sendMessageGTI9100ICS(String number, String msg) throws Exception {
    SmsManager m = SmsManager.getDefault();

    Class aclass[] = new Class[9];

    aclass[0] = String.class;
    aclass[1] = String.class;
    aclass[2] = ArrayList.class;
    aclass[3] = ArrayList.class;
    aclass[4] = ArrayList.class;
    aclass[5] = Boolean.TYPE;
    aclass[6] = Integer.TYPE;
    aclass[7] = Integer.TYPE;
    aclass[8] = Integer.TYPE;

    Method method = m.getClass().getMethod("sendMultipartTextMessage", aclass);

    Object aobj[] = new Object[9];
    aobj[0] = number;
    aobj[1] = null;
    aobj[2] = m.divideMessage(msg);
    aobj[3] = null;
    aobj[4] = null;
    aobj[5] = Boolean.valueOf(false);
    aobj[6] = Integer.valueOf(0);
    aobj[7] = Integer.valueOf(0);
    aobj[8] = Integer.valueOf(0);

    method.invoke(m, aobj);
}

这篇关于Android的sendTextMessage将在exceution两个相同的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 15:10