开发者

how to stop BroadcastReceiver if app is closed

开发者 https://www.devze.com 2023-04-11 18:37 出处:网络
I defined in my Manifest a Broadcast Receiver to control if the internet connection is active or no开发者_JAVA百科t.

I defined in my Manifest a Broadcast Receiver to control if the internet connection is active or no开发者_JAVA百科t. If there is no internet the app starts an Intent and opens a new Activity.

The problem is that if I close the app and I lose the internet connection the Activity pops up!

How do I stop the broadcast receiver when the app is closed?

Thanks


BroadcastReceiver is not closing. He may works even your app is closed. Try to add special boolean statements in your onReceive method.

Sample: if app closed - don't do code in onReceive

onReceive(){
    if(isAppRunning){
           // do something if app is running
    }
}


If you want the broadcast receiver to act only when the activity is running, you might need to consider registering inside the activity.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号