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 6958035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:05:18+00:00 2026-05-27T15:05:18+00:00

I am attempting to make an application that reads text messages. The application works

  • 0

I am attempting to make an application that reads text messages. The application works fine, when I get a text message, the message is displayed in a toast along with the phone number. The problem is, even with the application closed, ie not in the foreground, it still shows the toast when I get a text message. I have used a task killer application, and it still shows the toast. The only way to not show the toast is to unistall the application. I am using this website as a tutorial

http://www.apriorit.com/our-company/dev-blog/227-handle-sms-on-android

I have done everything in the tutorial except for the encryption.

Any help is appreciated!

Thanks,
Chris

  • 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-27T15:05:18+00:00Added an answer on May 27, 2026 at 3:05 pm

    That’s the correct behavior actully. Every time you get a message, the system sends a SMS broadcast. Since your application declares in it’s manifest that it wants to receive such broadcasts, a new instance of your BroadcastReceiver will be created and executed every time.

    If you want to execute the receiver only at certain times (in this case when your app is in the foreground), you have to register and unregister it dynamically in code instead of the manifest by using Context.registerReceiver() and Context.unregisterReceiver().

    How to do this exactly?

    Here’s a short example. I’ll assume that you have written your own class that extends BroadcastReceiver and handles stuff in onReceive(). The name of this class in this example is SmsReceiver, like in the linked tutorial.

    Our goal is to receive broadcasts only when one activity is in the foreground, which means you should also have one class that extends Activity and displays UI like a normal app.

    First of all we need an actual instance of the receiver as a class member. Add something like this to your activity class:

    private SmsReceiver smsReceiver = new SmsReceiver();
    

    Sidenote: That’s actually one of the main differences between registering in the manifest and in code:

    • In code you create a receiver instance by yourself
    • When registered in the manifest, the system generates the instances for you in the background

    Alright, great. Now we just have to register and unregister this receiver when the activity comes into the foreground and goes out of it. Have a look at the diagram in the Activity class doc, the framework methods called in these events are onResume() and onPause().

    Add the following lines to your onResume() method:

    @Override 
    public void onResume() {
    
        IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(smsReceiver, filter);
    }
    

    What we did here is actually pretty simple. It’s the code-equivalent of the manifests <receiver /> tag. We created an intent filter with a broadcast that we like to receive and register our receiver with it.

    The next step is to unregister in onPause(). Again, either add this line or create onPause() if you have not yet.

    @Override
    public void onPause() {
        unregisterReceiver(smsReceiver);
    }
    

    Pretty straightforward – take our receiver instance and unregister it when the app is about to go into the background. And that’s all the magic, everything should work as intended. Don’t forget to delete the whole <receiver /> tag in your manifest though, when you work with your existing code. Otherwise you would register your receiver in two ways.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to make a custom ContentProvider so that more than one application (activity?)
I am attempting to make it so that we can have our application to
I'm attempting to code an application that reads in the values from an IMU.
Attempting to make a NSObject called 'Person' that will hold the login details for
I'm attempting to port an application that plays chiptunes (NSF, SPC, etc) music files
I am attempting to make an application capable of running on both Sql Server
I'm attempting to make my networked application work locally (with both the server and
I am attempting to implement a quiet uninstall of my application. This works great
I've got a web application that controls which web applications get served traffic from
I am attempting to write an application that automatically changes the proxy server based

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.