Android BroadcastReceiver application doesn't die when application is closed

╄→гoц情女王★ 提交于 2021-02-20 04:23:07

问题


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:


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.




回答2:


You can think of a toast as a sort of system notification. So it will display regardless of whether any Activity is in the foreground. Also, when you extend BroadcastReceiver, your class will still receive the intents as long as your process is alive. A BroadcastReceiver is not tied to any foreground Activity.



来源:https://stackoverflow.com/questions/8584445/android-broadcastreceiver-application-doesnt-die-when-application-is-closed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!