Note that there are some explanatory texts on larger screens.

plurals
  1. POEnable and disable a Broadcast Receiver
    text
    copied!<p>I try to enable and disable a broadcast receiver by using this PackageManager method:</p> <pre><code>setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); </code></pre> <p>The broadcast receiver is registered in the manifest. The receiver works fine but when i try to disable it, it still receives the broadcast messages. When i disable the receiver in the manifest by "android:enabled="false"", the receiver does not receive anything but I can not enable it.</p> <p>I call the method from inside a service.</p> <pre><code> PackageManager pm = getApplicationContext().getPackageManager(); ComponentName componentName = new ComponentName("com.app", ".broadcast_receivers.OnNetworkChangedReceiver"); pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); </code></pre> <p>Android manifest:</p> <pre><code> &lt;receiver android:name=".broadcast_receivers.OnNetworkChangedReceiver" android:enabled="true"&gt; &lt;intent-filter&gt; &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE"/&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; </code></pre> <p>The Receiver</p> <pre><code>public class OnNetworkChangedReceiver extends BroadcastReceiver { private static final String TAG = "OnNetworkChangedReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "in OnNetworkChanged"); } } </code></pre> <p>I also called the method from inside an Activity yesterday. I thought it worked but today nothing works anymore. Could it be that there is sometimes a big delay in the intent (android.net.conn.CONNECTIVITY_CHANGE) that I misinterpreted yesterday as disabling the receiver?</p> <p>Is the approach with the PackageManager the right direction or is there a basic error in the idea?</p> <p>Thanks a lot, Sven</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload