Question

I have this application an AppWidget wherein when the user clicks it, it will send an SMS. Sending SMS with the click of the widget works fine. Now, I want to know if the sending of the SMS is successful or failed. How will I do that?

Here is the code in sending SMS

String sms2 = id + " " + name + " " + cn.getName() + " " + message
                                    + " " + lati + " " + longi 
                                    + " https://maps.google.com/?q=" + lati + "," + longi + " -alertoapp";
                            String cp = cn.getPhoneNumber();
                            PendingIntent piSent=PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
                            PendingIntent piDelivered=PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
                            SmsManager sms = SmsManager.getDefault();
                            sms.sendTextMessage(cp, null, sms2, piSent, piDelivered);

The Manifest

<receiver android:name=".Widget" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <!-- Broadcast Receiver that will also process our self created action -->
            <action android:name="de.thesmile.android.widget.buttons.ButtonWidget.ACTION_WIDGET_RECEIVER"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" />
    </receiver>

I want to use this method below for this works on toasting if the SMS is sent or not but the problem is registerReceiver method is not available in the BroadCastReceiver class.

 switch (getResultCode()) {
            case Activity.RESULT_OK:
                clear1();
                clear2();
                clear3();
                clear4();
                Toast.makeText(getBaseContext(), "SMS has been sent", Toast.LENGTH_SHORT).show();
                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;
            default:
                Toast.makeText(getApplicationContext(), "Coordinates is null, Try again", Toast.LENGTH_LONG).show();
                break;
            }

        }
    };
    smsDeliveredReceiver=new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            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;
            }
        }
    };
    registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
    registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));

Any suggestions guys?

Was it helpful?

Solution

To those encountering the same problem with me, I have fixed this with the help of this

So here it is

smsSentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context ctx, Intent arg1) {
        String msg = "";
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                msg = "SMS has been sent";
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                msg = "Generic Failure";
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                msg = "No Service";
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                msg = "Null PDU";
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                msg = "Radio Off";
                break;
            default:
                msg = "Coordinates is null, Try again";
                break;
        }
        Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }
};

smsDeliveredReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context ctx, Intent arg1) {
        String msg = "";
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                msg = "SMS Delivered";
                break;
            case Activity.RESULT_CANCELED:
                msg = "SMS not delivered";
                break;
        }
        Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }
};

ctx.getApplicationContext()
    .registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
ctx.getApplicationContext()
    .registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));

Conclusion: So, to register the receiver inside the BroadcastReceiver class.

Instead of registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));

Use context.getApplicationContext().registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top