Search code examples
androidbroadcastreceiver

Restrict Broadcast Receiver to application


I am working on broadcast receiver and stuck in a problem.

I am receiving a broadcast receiver in Manifest file.

<receiver class=".MyClass" android:name=".MyClass">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
    </receiver>

this is working fine and it is calling MyClass whenever there is change in connectivity.

Now the problem is whenever my application is not running still this class will receive broadcast receiver. I want it to receive whenever the application is running.

I tried it by extending BroadcastReceiver registering and unregistering broadcast in that class file and it works. But i want to achieve the same by Manifest file.

My problem will solve if it is not receiving anything when application is not opened.


Solution

  • What you are talking is not possible. The whole purpose of having the intent filter in the manifest is being able to receive the intent whether or not your application is running. The only way to do what you want to is by registering/unregistering the receiver from the code [registerReceiver]