Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9207705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:26:47+00:00 2026-06-18T00:26:47+00:00

When I pass extras to a PendingIntent, the message is never received by the

  • 0

When I pass extras to a PendingIntent, the message is never received by the sendBroadcastReceiver, because the onReceive() method of this BroadcastReceiver is never called. Why is this happening?

public class MainActivity extends Activity {
    private static String SENT = "SMS_SENT";
    private static String DELIVERED = "SMS_DELIVERED";
    private static int MAX_SMS_MESSAGE_LENGTH = 160;

    private static Context mContext;
    private BroadcastReceiver sendBroadcastReceiver, deliveryBroadcastReceiver;
    private final String DEBUG_TAG = getClass().getSimpleName().toString();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // ---when the SMS has been sent---
        sendBroadcastReceiver = new BroadcastReceiver() {

            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS 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;
                    }
            }
        };
        // ---when the SMS has been delivered---
        deliveryBroadcastReceiver = new BroadcastReceiver() {
            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;
                }
            }
        };

        Log.d(DEBUG_TAG, "onCreate() : Register receivers");
        registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
        registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

        mContext = getApplicationContext();

        setContentView(R.layout.main);
        // ... Bind views and set up onClick listeners ( for example, one to call sendSMS() )

    }

    @Override
    protected void onDestroy() {
        // Unregister receivers
        Log.d(DEBUG_TAG, "onDestroy() : Unregister receivers");
        if (sendBroadcastReceiver != null) {
            unregisterReceiver(sendBroadcastReceiver);
            sendBroadcastReceiver = null;
        }
        if (deliveryBroadcastReceiver != null) {
            unregisterReceiver(deliveryBroadcastReceiver);
            deliveryBroadcastReceiver = null;
        }
        super.onDestroy();
    }

    // ---sends an SMS message to another device---
    public static void sendSMS(String phoneNumber, String message) {
        Intent sendIntent = new Intent(SENT);
        sendIntent.putExtra("extra_key", "extra_value"));

        PendingIntent piSent = PendingIntent.getBroadcast(mContext, 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent piDelivered = PendingIntent.getBroadcast(mContext, 0, new Intent(DELIVERED), 0);
        SmsManager smsManager = SmsManager.getDefault();

        int length = message.length();
        if (length > MAX_SMS_MESSAGE_LENGTH) {
            ArrayList < String > messagelist = smsManager.divideMessage(message);
            smsManager.sendMultipartTextMessage(phoneNumber, null, messagelist, null, null);
        } else 
            smsManager.sendTextMessage(phoneNumber, null, message, piSent, piDelivered);
        }
   }

   //More methods of MainActivity ... 

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:26:48+00:00Added an answer on June 18, 2026 at 12:26 am

    I guess there are some issues with registering and unregistering in the code. Instead I registered the two BroadcastReceivers in the AndroidManifest.xml file and was able to successfully pass extras.

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myexample" android:versionCode="1" android:versionName="1.0">
        <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="17" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.SEND_SMS" />
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.WRITE_SMS" />
        <uses-permission android:name="android.permission.RECEIVE_MMS" />
        <uses-permission android:name="android.permission.WRITE" />
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <application android:debuggable="true" android:icon="@drawable/ic_launcher_icon"
    android:label="@string/app_name">
            <activity //Main activity... 
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity //Activity 2 ... </activity>//More acitivies ... 
    
            // Send Receiver
            <receiver android:name="com.sendit.receivers.SendBroadcastReceiver">
                <intent-filter>
                    <action android:name="SMS_SENT" />
                </intent-filter>
            </receiver>
            //Delivery Receiver
            <receiver android:name="com.sendit.receivers.DeliveryBroadcastReceiver">
                <intent-filter>
                    <action android:name="SMS_DELIVERED" />
                </intent-filter>
            </receiver>
        </application>
    </manifest>
    

    SendBroadcastReceiver.java

    public class SendBroadcastReceiver extends BroadcastReceiver {
        private final String DEBUG_TAG = getClass().getSimpleName().toString();
        private static final String ACTION_SMS_SENT = "SMS_SENT";
    
        // When the SMS has been sent
        public void onReceive(Context context, Intent intent) {
    
            String action = intent.getAction();
    
            if (action.equals(ACTION_SMS_SENT)) {
    
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "SMS Sent", Toast.LENGTH_SHORT).show();
                        Bundle b = intent.getExtras();
                        Log.d(DEBUG_TAG, "sendBroadcastReceiver : b is " + b);
                        if (b != null) {
                            String value = b.getString("extra_key");
                            Log.d(DEBUG_TAG, "sendBroadcastReceiver : value is " + value);
                        }
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT)
                            .show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(context, "No service", Toast.LENGTH_SHORT)
                            .show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show();
                        break;
                }
    
            }
        }
    
    }
    

    DeliveryBroadcastReceiver.java

    public class DeliveryBroadcastReceiver extends BroadcastReceiver {
        private final String DEBUG_TAG = getClass().getSimpleName().toString();
        private static final String ACTION_SMS_DELIVERED = "SMS_DELIVERED";
    
        // When the SMS has been delivered
        public void onReceive(Context context, Intent intent) {
    
            String action = intent.getAction();
    
            if (action.equals(ACTION_SMS_DELIVERED)) {
    
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "SMS Delivered",
                        Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(context, "SMS not delivered",
                        Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }
    
    }
    

    MainActivity.java

    public class MainActivity extends Activity {
        private static String ACTION_SMS_SENT = "SMS_SENT";
        private static String ACTION_SMS_DELIVERED = "SMS_DELIVERED";
        private static int MAX_SMS_MESSAGE_LENGTH = 160;
    
        private static Context mContext;
        private final String DEBUG_TAG = getClass().getSimpleName().toString();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            mContext = getApplicationContext();
    
            setContentView(R.layout.main);
            // ... Bind views and set up onClick listeners ( for example, one to call sendSMS() )
    
        }
    
        // ---sends an SMS message to another device---
        public static void sendSMS(String phoneNumber, String message) {
            Intent sendIntent = new Intent(ACTION_SMS_SENT);
            sendIntent.putExtra("extra_key", "extra_value"));
    
            PendingIntent piSent = PendingIntent.getBroadcast(mContext, 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piDelivered = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_SMS_DELIVERED), 0);
            SmsManager smsManager = SmsManager.getDefault();
    
            int length = message.length();
            if (length > MAX_SMS_MESSAGE_LENGTH) {
                ArrayList < String > messagelist = smsManager.divideMessage(message);
                smsManager.sendMultipartTextMessage(phoneNumber, null, messagelist, null, null);
            } else 
                smsManager.sendTextMessage(phoneNumber, null, message, piSent, piDelivered);
            }
        }
    
        //More methods of MainActivity ... 
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem to pass extras between an Activity and a FragmentActivity. This
I am trying to pass this: Intent i=new Intent(ctx,SpendingsDetails.class); extras.putString(SpendingAmount, 1); extras.putString(SpendingDescription,2); extras.putString(SpendingDate,3); i.putExtras(extras);
Pass a []byte into a template as the body of a message post on
I have multiple variables to pass from one activity to another. I have this
I create a notification with an activity like this, public static void buildNotification(Context context,
I have this ConstantData class which holds my JSON indexes, I need to pass
Passing parameters works with 2 normal activities, but when I try to pass this
I have a method public boolean findANDsetText (String Description, String ... extra ) {
Something I've always wondered about rails is the ability to pass extra data to
I need to pass an extra parameter :mobilejs => true from jQuery to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.