问题
I've got an app with the following in the manifest
<receiver android:name="com.redacted.BroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<activity android:name="com.redacted.activity.UserLaunch" android:label="@string/app_name" android:launchMode="standard" android:clearTaskOnLaunch="true" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If I install and run the app via Android Studio, then reboot the phone then the BroadcastReceiver's onReceive()
gets called as a consequence of connectivity changes settling down after boot up.
However if I build an .apk and then install that, then on device reboot the onReceive()
is not getting called. (There is a Log.d()
statement in the onReceive
which I'm looking for in logcat
after bootup, it appears with the first installation method but not with the second).
Why is there this difference?
回答1:
Apps are installed in a so-called "stopped state". It takes an explicit Intent
starting up one of the app's components to move out of the stopped state. Usually, that's running the launcher activity. While in the stopped state, no registered broadcast receivers will work.
So, when you run from Android Studio, the launcher activity moves your app out of the stopped state, and all is good. Installation by some other means would require you to run the launcher activity yourself to move out of the stopped state.
来源:https://stackoverflow.com/questions/32895699/app-install-via-android-studio-starts-at-boot-but-not-if-installed-as-an-apk