Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-Application BroadcastReceiver

Can anybody tell me a way to create an in-application BroadcastReceiver? I have created BroadcastReceiver which Toasts a message. It works even when the application is in background state, I want it to work only when application is in foreground.

  public class MainActivity extends Activity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                this.registerReceiver(this.mConnReceiver, new IntentFilter(
                        ConnectivityManager.CONNECTIVITY_ACTION));
            }

            private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {

                public void onReceive(Context context, Intent intent) {
                    boolean noConnectivity = intent.getBooleanExtra(
                            ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                    String reason = intent
                            .getStringExtra(ConnectivityManager.EXTRA_REASON);
                    boolean isFailover = intent.getBooleanExtra(
                            ConnectivityManager.EXTRA_IS_FAILOVER, false);

                    NetworkInfo currentNetworkInfo = (NetworkInfo) intent
                            .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                    NetworkInfo otherNetworkInfo = (NetworkInfo) intent
                            .getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

                    if (currentNetworkInfo.isConnected()) {
                        System.out.println("Connected");
                        Toast.makeText(getApplicationContext(), "Connected",
                                Toast.LENGTH_LONG).show();
                    } else {
                        System.out.println("Not Connected");
                        Toast.makeText(getApplicationContext(), "Not Connected",
                                Toast.LENGTH_LONG).show();
                    }
                }
            };

        }

So here is my code which is checking network state and generating a BroadcastReceiver. I haven't added anything in manifest.
like image 460
Rishabh Srivastava Avatar asked Sep 11 '13 08:09

Rishabh Srivastava


2 Answers

There are a number of ways to do this. The first 2 I can think of are like this:

  1. If your <receiver> is declared in the manifest with an <intent-filter>, you can enable it and disable it from your application so that it is only enabled when the app is in the foreground. To do this, initially set the receiver disabled by adding android:enabled="false" to the manifest entry for <receiver>. Now, when your app is running, in onResume() you want to enable the receiver. Use PackageManager.setComponentEnabledSetting() to do this. When your activity goes to the background, in onPause() you can disable the receiver again.

  2. Dynamically register and unregister the receiver. For this you don't need to declare the receiver in the manifest. In your application, create an instance of your BroadcastReceiver and in onResume() call registerReceiver() with the appropriate intent filter. When the app goes to the background, in onPause() call unregisterReceiver() to remove it. The receiver will only receive calls to onReceive() while the app is in the foreground.

like image 123
David Wasser Avatar answered Oct 04 '22 08:10

David Wasser


see Context.registerReceiver() and Context.unregisterReceiver()

like image 42
pskink Avatar answered Oct 04 '22 08:10

pskink