In my AndroidManifest.xml I have the following...
<service
android:name=".MyIntentService" >
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />"
<action android:name="com.mycompany.myapp.TEST" />
</intent-filter>
</service>
<receiver
android:name=".MyNetworkMonitor" >
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />"
</intent-filter>
</receiver>
Ideally I'd like the IntentService to 'wake' and interpret the Intent(s) broadcast by the system when either the wi-fi or mobile internet services change state....but it doesn't.
The Broadcas开发者_开发知识库tReceiver does receive the Intents, however, and I can get it to send a broadcast intent (com.mycompany.myapp.TEST
) to wake my IntentService and tell it there has been a change in network state.
So the question is, why won't my IntentService wake from the system intent broadcasts informing of network state change but it will wake from a broadcast from my BroadcastReceiver. Confused.
EDIT: This is what you get for trying to code at nearly 5am (oops). For some reason I thought the com.mycompany.myapp.TEST
intent was working as a broadcast.
What I'm actually doing is Intent i = new Intent ("com.mycompany.myapp.TEST")
and then using startService(i)
which works because of the <intent-filter>
entry (even without explicitly specifying MyIntentService.class
in the Intent). This is what was confusing the issue.
You can think of the Intent
message bus as having three channels:
- You can start an activity
- You can start or bind to a service
- You can send a broadcast
These three channels are completely independent of one another. You cannot have a service receive a broadcast, as you are trying to do here, any more than you can have a service receive a start-activity Intent
.
Please delete the <intent-filter>
from the service. Then, have your BroadcastReceiver
call startService()
on your service (using new Intent(context, MyIntentService.class)
) when the broadcast arrives.
精彩评论