问题
I have a really simple problem which is driving me nuts. I am creating a BroadcastReceiver, declaring it on the manifest but it just wont run. I'm trying to make it trigger on device boot. Here's the code:
package com.vullnetdyla.bcreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("ftw", "It worked");
}
}
And the manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vullnetdyla.bcreceiver"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<receiver android:name="com.vullnetdyla.bcreceiver.Receiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
回答1:
Do you have some Activity which user can launch?
If not this is your problem! Since android 3.1 after installation application (package to be more specific) is in stopped state and doesn't receive ANY broadcast. User have to launch it manually at lest once to make it work.
See section "Launch controls on stopped applications" in release notes of Android 3.1.
See also flags FLAG_INCLUDE_STOPPED_PACKAGES, FLAG_EXCLUDE_STOPPED_PACKAGES.
来源:https://stackoverflow.com/questions/11865434/android-broadcastreceiver-wont-work