2

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.

Harshad
  • 7,904
  • 3
  • 24
  • 42

3 Answers3

3

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]

advantej
  • 20,155
  • 4
  • 34
  • 39
  • thank you for reply.is there other way round that I can check if application is running or not when I receive the broadcast, I means in the same class which is receiving that broadcast can i check it? – Harshad Apr 20 '11 at 04:59
  • Your broadcast receiver class is the part of the application. So when it starts that means even if any other components of your application are not running, the application is running ! – advantej Apr 20 '11 at 05:01
  • 1
    If you want to keep your receiver in manifest, you can also enable/disable reciever from Packagemanager. – EvilDuck Mar 05 '13 at 13:40
1

The question was asked a long time ago but in case some one landed on this page while searching, It is possible to register and unregister broadcast receiver from code instead of doing that from manifest file. (Checking the Networking Connectivity using BroadcastReceiver in Android)

Community
  • 1
  • 1
0

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

Here how I understand your question and appropriate answer.

android:enabled

Whether or not the broadcast receiver can be instantiated by the system — "true" if it can be, and "false" if not. The default value is "true".

If you want to enable your receiver at runtime, you can set the state to disabled initially. You can do so in the manifest file:

<receiver
android:name=".YourReceiver"
android:enabled="false" >
<!-- your intent filter -->
</receiver>

Source: http://developer.android.com/guide/topics/manifest/receiver-element.html#enabled

http://www.grokkingandroid.com/enabling-and-disabling-broadcastreceivers/

johnkarka
  • 365
  • 2
  • 14
  • If you require that the app only receive some broadcast when it's running, it's best to register that broadcast receiver in code at runtime and unregister when not needed. It more clearly achieves the purpose than enabling and disabling the entry in the manifest whose sole purpose is to allow the system to send the intent when your app is not running. – Sojurn Feb 05 '14 at 06:35