问题
None of this broadcast receivers are not triggered, but in another my application is working where I used to check network state. I have nothing else to say about this problem.
package monitor;
import android.content.BroadcastReceiver;
public class Monitor extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("jupi");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="monitor"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<service
android:name=".MonitorService">
</service>
<receiver
android:name=".Monitor">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
回答1:
If you are seeing this behaviour on Android 3.1 or later, you are probably seeing the results of this change:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
Especially read this:
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications.
Since your application only contains a service and receivers, the user will never start this application and therefore if will remain in the "stopped" state, preventing it from seeing any broadcast intents.
When you added the Activity to your application, running it once was enough to remove the application from the "stopped" state so that your receivers were then eligible to receive broadcast intents. You actually don't need to dynamically register the receivers. It is enough to just have the user "start" the application once after installation.
来源:https://stackoverflow.com/questions/11273770/broadcastreceiver-isnt-working