Reputation: 339
I've revewied the answer to How does android resolve multiple entries in the AndroidManifest for the same broadcast receiver with different permissions and intent-filters? but my question is slightly different.
If I have 2 classes in my app, 1 to start a service and and another to stop the service based on different system events, can they both receive the same broadcast.
I want to either start or stop my service depending on whether the user switches on or off airplane mode. Will onReceive() in both classes by fired with the following entries in my manifest.
<receiver android:name=".StartService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>"
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
<receiver android:name=".StopService" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
<!-- for power testing only -->
<!--<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>-->
</receiver>
Upvotes: 2
Views: 2257
Reputation: 11721
If i remember rightly you register in a class listeners to capture those events. I.e. Connectivity Manager. So you can either have a single class running as a service that captures the change in connectivity ( at which point you can detect if you're in airplane mode) and do whatever, or register in the Activity that is currently active to listen out for those events as well. Personaly i'd go for a service running in the background in nearly all scenarios.
Edit: Having read your question it sounds like your starting a service with one class and want to kill that service with the other class. In which case, i don't think isn't necessarily the best way of doing it. Instead in the service you've started, register to capture changes in connectivity and get the service to stop itself.
Upvotes: 1
Reputation: 22592
I'm not sure if that is possible but an alternative method would be to catch it in the StartService receiver and then inside that check the status of your Service. If your service is already started, launch your on broadcast and catch that within your StopService?
Upvotes: 2