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

  • Home
  • SEARCH
  • 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 6565469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:05:53+00:00 2026-05-25T14:05:53+00:00

My app uses a BroadcastReceiver to get sms in this way: SmsBR.java public class

  • 0

My app uses a BroadcastReceiver to get sms in this way:

SmsBR.java

public class SmsBR extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[])bundle.get("pdus");
            final SmsMessage[] messages = new SmsMessage[pdus.length];

            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            }
            if (messages.length > 0)
                //doSomething();
            }
        }
    }
}

Manifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Activity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter> 
    </activity>

    <receiver android:name=".SmsBR">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
    </receiver>
</application>    

In this way SmsBR is always ON. I want to register it when a service starts (onCreate()) and unregister it when the service is destroyed (onDestroy()). How can I do this?

  • 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-05-25T14:05:53+00:00Added an answer on May 25, 2026 at 2:05 pm

    Edit:

    For an Activity:

    In order to register your broadcast receiver from within your app, first, remove the <receiver> tag from your AndroidManifest.xml file. Then, call registerReceiver(BroadcastReceiver receiver, IntentFilter filter) in your onResume(). Use unregisterReceiver(BroadcastReceiver receiver) in your onPause() to unregister the Broadcast receiver.

    For a Service:

    Remove the receiver tag from the manifest file. You can then register your Broadcast receiver with the same method in the onCreate() and unregister in the onDestroy().

    EDIT: Sample Code:

    public class MyActivity extends Activity {
      private final BroadcastReceiver mybroadcast = new SmsBR();
    
      public void onResume() {
        super.onResume();
    
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(mybroadcast, filter);
      }
    
      public void onPause() {
        super.onPause();
    
        unregisterReceiver(mybroadcast);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app uses a WebRequest at certain points to get pages from itself. This
Inside of the onReceive(Content context, Intent intent) method of my public class MediaButtonIntentReceiver extends
hey all when installing my app to the emulator I get this: ERROR/AndroidRuntime(465): java.lang.RuntimeException:
I try to develope a simple Broadcastreceiver. Here's my code: public class MyClass extends
This is my code: Activity: @Override public void onCreate(Bundle savedInstanceState) { ... Intent intent
My app uses an API from an external JAR file. This JAR file has
My app uses a logging class which is invoked by each module as it
I am using BroadcastReceiver class for receiving the sms. My main class code is:
I am currently working on an app that uses a broadcastreceiver to check for
Our app uses a component that requires a license file in the directory with

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.