第一次安装 .apk 时如何启动服务

第一次安装 .apk 时如何启动服务

问题描述:

在我的应用程序中,我没有任何 UI 部分,因此我需要在应用程序安装到设备上后立即启动服务.我看到很多链接,答案是它不可能,但我想这肯定是可能的.看看 Android Market 上的 PlanB 应用程序,它确实满足了我的要求.下面是我如何尝试的清单文件,但根本没有调用该服务.所以,让我知道在安装应用程序时启动服务的最佳方式是什么.

In my Application I am not having any UI part, so I need to start a Service as soon as the Applicaton gets installed on the Device. I saw many links from which the answer was that its not possible but I guess it is surely possible. Just have a look at PlanB Application on the Android Market that does fulfil my requirement. Below is my Manifest file how I tried, but the Service was not called at all. So, let me know what is the best possible way to start a Service when the Application gets Installed.

更新

我也尝试使用 android.intent.action.PACKAGE_ADDED 它可以很好地检测其他应用程序的包,但不能正常检测自身.

I also tried using android.intent.action.PACKAGE_ADDED it works fine for detecting the Package for the other Applications but not for itself.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.auto.start"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:icon="@drawable/ic_launcher" >

        <service android:name=".MyService">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </service>

        <receiver android:name=".BootUpReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <action android:name="android.intent.action.PACKAGE_INSTALL" />
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

幸运的是,Plan B 不适用于 Android 3.1+,在 XOOM 和 Galaxy Nexus 上进行了测试.

Fortunately, Plan B does not work on Android 3.1+, as tested on a XOOM and a Galaxy Nexus.

B 计划所做的是利用一个安全漏洞,该漏洞可能会被路过式恶意软件利用,这正是 Android 阻止它再发生的原因.

What Plan B does is exploit a security hole that could be used by drive-by malware, which is specifically why Android prevents it from happening anymore.

更新

澄清:正如 inazaruk 发布的那样,我对其他答案发表了评论,所有应用程序在安装后都处于停止"状态.这与用户从设置"应用程序强制停止应用程序后应用程序结束的状态相同.在此停止"状态下,应用程序不会出于任何原因运行,除非手动启动活动.值得注意的是,在用户手动运行应用程序之前,不会调用 BroadcastReceviers,无论它们注册的是什么事件.

To clarify: As inazaruk posted and I put into comments on other answers, all applications, upon installation, are placed in a "stopped" state. This is the same state that the application winds up in after the user force-stops the app from the Settings application. While in this "stopped" state, the application will not run for any reason, except by a manual launch of an activity. Notably, no BroadcastReceviers will be invoked, regardless of the event for which they have registered, until the user runs the app manually.

这个块涵盖了他们以前利用的远程安装和运行的 B 计划方案.毕竟,这样一来,任何拥有被黑 Google 帐户的人都将面临设备被感染的风险,而且无需手动操作.

This block covers the Plan B scenario of remote-install-and-run, which they were taking advantage of previously. After all, with that, anyone with a hacked Google account would be at risk of having their device infected, hands-free as it were.

所以,当 OP 说:

我需要在应用程序安装到设备上后立即启动服务

I need to start a Service as soon as the Applicaton gets installed on the Device

OP 将不成功,需要重新设计应用程序以避免这种所谓的需要".

the OP will be unsuccessful and will need to redesign the application to avoid this purported "need".