java 应用程序关闭后广播接收器仍在运行 - Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16429354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:55:31  来源:igfitidea点击:

Broadcast Receiver Still Running after app close - Android

javaandroidsmshandle

提问by Loren Kuich

I'm using some code from this tutorialon how to receive SMS messages:

我正在使用本教程中有关如何接收 SMS 消息的一些代码:

The code works perfectly, and does exactly what I want.

代码完美运行,完全符合我的要求。

I am having 1 issue with it.

我有 1 个问题。

I want my app to run in the background, but when it's closed I want it to stop intercepting SMS messages.

我希望我的应用程序在后台运行,但是当它关​​闭时,我希望它停止拦截 SMS 消息。

My question is, why is my app still intercepting SMS messages after it's been closed?

我的问题是,为什么我的应用在关闭后仍然拦截短信?

I guess I have to find a "on close" handler and close the Broadcast Receiver then. (If there is a "on close" event handler..?).

我想我必须找到一个“关闭”处理程序然后关闭广播接收器。(如果有一个“关闭时”事件处理程序......?)。

If anyone can provide some insight I would be very grateful. Thanks!

如果有人可以提供一些见解,我将不胜感激。谢谢!

回答by ianhanniballake

By putting your BroadcastReceiverin your Manifest, it, by default, is always active. Therefore if you want it to only run while your Activityis shown, you'll want to enable/disable your BroadcastReceiverwhen your Activityresumes/pauses:

通过将你的BroadcastReceiver放在你的清单中,它在默认情况下始终处于活动状态。因此,如果您希望它仅在Activity显示时运行,则需要BroadcastReceiverActivity恢复/暂停时启用/禁用:

public void onResume()
{
    ComponentName component=new ComponentName(this, TextMessageReceiver.class);
    getPackageManager()
        .setComponentEnabledSetting(component,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

public void onPause()
{
    ComponentName component=new ComponentName(this, TextMessageReceiver.class);
    getPackageManager()
        .setComponentEnabledSetting(component,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
}

The alternative, is to declare your BroadcastReceiverin your Activityand then call registerReceiverand unregisterReceiverin the same lifecycle events.

另一种方法是BroadcastReceiver在您的Activity然后调用registerReceiverunregisterReceiver在相同的生命周期事件中声明您。

回答by JRowan

ive looked around on here and this is the conclusion ive come to

我环顾四周,这是我得出的结论

try{
        unregisterReceiver(mybroadcast);
    }catch(IllegalArgumentException e){
        Log.d("reciever",e.toString());
    }

i would say override onDestroy() and put it in there

我会说覆盖 onDestroy() 并将其放在那里

回答by Daniel Gabay

i would say override OnTerminate() on your application object and unregister your reciever there.

我会说在您的应用程序对象上覆盖 OnTerminate() 并在那里取消注册您的接收器。

@Override
public void onTerminate() {
    unregisterReceiver(mybroadcast);
    super.onTerminate();
}