Sending the same SMS twice

2019-07-16 12:09发布

I'm trying to make an sms android app, but i'm getting an error that i've never seen before, even in google i haven't found anything like that. So, if you could help me... i'll be glad

For some reason, the program is sending two messages (the same sms) at the same time. But it just happens in production. When i'm using the simulator, everything works fine! it Sends the sms just once ...

I've tried many phones and many phone operators but the error always happens. I really dont know how to discover what is happening because it happens only in real phones and not in simulator...

code follows below:

private void sendSMS(String phoneNumber, String message, Context context) {
    ContextWrapper cw = new ContextWrapper(context);
    Context baseContext = cw.getBaseContext();

    Intent intentSMS = new Intent(baseContext, SMSManagerService.class);
    intentSMS.putExtra("celNumber", phoneNumber);
    intentSMS.putExtra("textMessage", message);

    Random s = new Random(System.currentTimeMillis());
    PendingIntent pendingIntent = PendingIntent.getService(cw, s.nextInt(), intentSMS, PendingIntent.FLAG_ONE_SHOT);
    try {
        pendingIntent.send();
    } catch (CanceledException e) {
    }
}

SMSManagerService.class

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Bundle intentExtras = intent.getExtras();
    if (intentExtras != null) {
        String phoneNumber = intentExtras.getString("celNumber");
        String message = intentExtras.getString("textMessage");

        if (isContentValid(phoneNumber, message)) {
            sendSMS(phoneNumber, message);
        }
    }

    return super.onStartCommand(intent, flags, startId);
}

 private void sendSMS(String phoneNumber, String message) {
    sentPI = registerSMSSent(phoneNumber, message);
    deliveredPI = registerSMSdelivered();

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

so.. anyone... any hint?!

Thx

4条回答
家丑人穷心不美
2楼-- · 2019-07-16 12:26

Maybe You will find the answer HERE and THIS kind of a tutorial can be useful too

查看更多
再贱就再见
3楼-- · 2019-07-16 12:26

Only to clarify the question and give an answer...

It's a bug on android, depending on phone and android version, that is running the sms application.

The bug is related here: Google code Android inssues

and one possible, thats works for me, is related here: Android sendTextMessage sends two identical messages on exceution, how to fix?

查看更多
姐就是有狂的资本
4楼-- · 2019-07-16 12:44

I don't immediate see something wrong here. Can you connect your pc with your android phone, put a line break somewhere in your code and debug it this way? This way you can check how it comes into SMSManagerService.class sendSMS twice.

查看更多
不美不萌又怎样
5楼-- · 2019-07-16 12:51

Hi :) Well I can be wrong but isn't pendingIntent.send(); sending the message? also, while in debuging mode, try to look what LogCat is saying :) Maybe you will find the answer there ;)

查看更多
登录 后发表回答