通过Android Studio进行的应用安装会在启动时启动,但如果以.apk的形式安装则不会启动 [英] App install via Android Studio starts at boot but not if installed as an .apk

查看:229
本文介绍了通过Android Studio进行的应用安装会在启动时启动,但如果以.apk的形式安装则不会启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

清单中有一个带有以下内容的应用程序

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>

如果我通过Android Studio安装并运行该应用程序,然后重新启动手机,则由于连接更改在启动后趋于稳定,因此会调用BroadcastReceiver的 onReceive().

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.

但是,如果我构建一个.apk然后安装它,那么在设备重启时,不会调用 onReceive().(启动后,我在 logcat 中寻找的 onReceive 中有一个 Log.d()语句,该语句与第一个安装方法,但不能与第二种方法配合使用).

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).

为什么会有这种区别?

推荐答案

应用程序以所谓的已停止状态"安装.它需要显式的 Intent 来启动应用程序的组件之一才能移出停止状态.通常,这正在运行启动器活动.在停止状态下,没有已注册的广播接收器将起作用.

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.

因此,当您从Android Studio运行时,启动器活动会将您的应用移出停止状态,一切都很好.通过其他方式进行安装将需要您自己运行启动器活动,以退出停止状态.

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.

这篇关于通过Android Studio进行的应用安装会在启动时启动,但如果以.apk的形式安装则不会启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆