简体   繁体   English

广播接收器在后台应用程序时不活动

[英]broadcast receiver not active when app in background

I have a broadcast receiver registered through manifest file. 我有一个通过清单文件注册的广播接收器。 Basically, it activates when internet connection is on/off. 基本上,当互联网连接开启/关闭时,它会激活。 It display Toast when it come on and also when it goes off. 它会在Toast出现时以及在它关闭时显示Toast。

However, I dont want this Toast to be displayed when my app in the background (a home button is pressed). 但是,当我的应用程序在后台(按下主页按钮)时,我不希望显示此Toast。 How can I do that? 我怎样才能做到这一点? I dont mind if the broadcast is still registered but I need a way to know my app is not visible so I disable the Toast. 我不介意广播是否仍然注册,但我需要一种方法来了解我的应用程序是不可见的所以我禁用了Toast。

Thank you very much 非常感谢你

    ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
        NetworkInfo active_nwInfo = (NetworkInfo) connManager.getActiveNetworkInfo();

if(active_nwInfo == null || !active_nwInfo.isConnected()) {
            //broadcast.putExtra("action", "no_connection");
            Toast.makeText(context, "No Internet Connection", Toast.LENGTH_LONG).show();


        }else {
            //broadcast.putExtra("action", "new_connection");
            Toast.makeText(context, "Internet Connection", Toast.LENGTH_LONG).show();


        }

You can create an Activity eg BaseActivity (which extends Activity ofcourse). 您可以创建一个Activity,例如BaseActivity (扩展Activity ofcourse)。 In onResume() and onPause() methods of this Activity , you can set a boolean variable as Anup Cowkur has done in his answer. 在此Activity的onResume()onPause()方法中,您可以设置一个布尔变量,就像Anup Cowkur在他的答案中所做的那样。

Now you can extend all your activities from BaseActivity instead of Activity class. 现在,您可以从BaseActivity而不是Activity类扩展所有活动 So in onReceive() function of your BroadcastReceiver , you can first check this boolean variable and show Toast only when it is "true". 因此,在BroadcastReceiver的 onReceive()函数中,您可以先检查此布尔变量,并仅在Toast为“true”时显示Toast。

These links are quite helpful : 这些链接非常有用:

Checking if an Android application is running in the background 检查Android应用程序是否在后台运行

Why BroadcastReceiver works even when app is in background ? 为什么BroadcastReceiver即使应用程序处于后台也能正常工作?

In your activity, unregister your receiver at onPause(), and register it again at onResume() 在您的活动中,在onPause()上取消注册您的接收器,并在onResume()上再次注册它

@Override
protected void onPause() {
    mLocalBroadcastManager.unregisterReceiver(mReceiver);
    super.onPause();
}

@Override
protected void onResume() {
    mLocalBroadcastManager.registerReceiver(mReceiver, filter);
    super.onResume();
}

I am just using LocalBroadcastManager for demo, change it to whichever that suits your receiver. 我只是使用LocalBroadcastManager进行演示,将其更改为适合您的接收器的那个。

Use a boolean flag to indicate whether app is in background or not: 使用布尔标志来指示app是否在后台:

boolean appIsInBackgorund = false;
  @Override
    protected void onPause() {
        appIsInBackgorund = true;
        super.onPause();
    }

    @Override
    protected void onResume() {
        appIsInBackgorund = false;
        super.onResume();
    }

Now, you can check this flag to determine if the app is in background state and determine whether to display or not display your toast. 现在,您可以检查此标志以确定应用程序是否处于后台状态,并确定是否显示您的祝酒词。

If you need the same flag in more than one activity, you can store it in SharedPreferences . 如果您需要在多个活动中使用相同的标志,则可以将其存储在SharedPreferences中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当APP在android中处于后台时如何激活广播接收器? - How to active Broadcast Receiver only when APP is in background in android? 当应用程序处于前台或活动状态时,广播接收器不起作用 - Broadcast receiver is not working when app on foreground or active 应用在后台运行时的Android Broadcast Receiver - Android Broadcast Receiver when app is running in background 应用程序在后台时的 Android 广播接收器 - Android broadcast receiver when the app is in background 当应用程序处于后台或未运行时,在“呼入电话”上不会调用广播接收器 - On Incoming Call when app is in background or app is not running the broadcast receiver is not called 当应用暂停或在后台时,避免接收广播接收器 - avoid to receive Broadcast Receiver when app is pause or in background 应用毁坏时的广播接收器 - Broadcast Receiver when app destroyed android广播接收器检测应用程序在后台更改 - android broadcast receiver to detect app in background change 应用程序进入后台时注销广播接收器 - Unregistering Broadcast Receiver when application goes to background 如何确定应用程序在运行时是否处于休眠状态或在后台运行时是否调用了广播接收器? - How can I determine if a broadcast receiver was invoked when the app was running as opposed to when the app was dormant or in the background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM