0

我正在尝试使用尽可能少的代码从我的 apk 启动外部服务。在 4.0 AVD 上测试包并验证 logcat 中的响应似乎给出了正确的结果;但是,在实际设备上它不会加载。实际上,它甚至似乎根本没有在 logcat 中列出。

这可能是我忽略的东西,只需要第二双眼睛来确认。

启动服务.java:

package com.winca.service.start;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartService extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent();
        i.setClassName("com.winca.service", "com.winca.service.StartService");
        context.startService(i);
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.winca.service.start"
    android:versionCode="13"
    android:versionName="1.3" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="com.winca.service.start.StartService">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

AVD 回应:

# getprop ro.build.fingerprint
generic/sdk/generic:4.0.2/ICS_MR0/229537:eng/test-keys
# logcat -c; sleep 1; am broadcast -a android.intent.action.BOOT_COMPLETED; sleep 20; logcat -d | grep winca
Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED }
Broadcast completed: result=0
W/ActivityManager(   80): Unable to start service Intent { cmp=com.winca.service/.StartService }: not found
#

我实际上期望“未找到”,因为包含该服务的实际软件包未安装在 AVD 上。由于系统 SharedUserID,我无法安装它。但是,至少我可以看到它正在尝试使用 AVD 加载它,而实际设备甚至没有在 logcat 消息中列出。

提供一些背景知识,当第三方启动器设置为默认值时,这个特定的 Android 设备不会加载“com.winca.service/.StartService”服务。这可以防止设备上的许多音频服务在被激活之前一直处于禁用状态。所以,我想也许可以快速打包来做这件事;而不是使用像 Tasker 之类的东西(由于某种未知原因,强制在此设备上关闭)。

4

2 回答 2

1

在您的组件之一响应显式Intent. 典型的工作方式是由用户运行您的一项活动。在此之前,在 Android 3.1+ 上,您的应用程序处于“停止状态”,没有清单注册的接收器将响应广播。

于 2014-01-22T21:34:33.910 回答
0

事实证明,将应用程序移动到 /system/app 目录绕过了正常安装的“停止状态”。无论如何,这就是意图,因为用户正在为这个特定的 Android 设备拼凑一个自定义 ROM。问题是我们一直在通过安装它来测试应用程序,这会导致“停止状态”并阻止它正常执行。但是,将应用程序移动到具有正确文件权限的 /system/app 目录允许 apk 按预期工作。

我想这对于大多数第三方开发情况来说是不切实际的,比如通过 Google Play 分发应用程序。为此,您需要遵守 CommonsWare 指出的允许用户在安装意图时至少启动一次的约定。

至于为什么它在 AVD 中运行,回想起来我怀疑它与最初通过 Eclipse 的 Run > Debug (F11) 机制安装它有关。我猜这使用了一种不同的安装方法,根据“dumpsys package {package.name}”将包标记为“stopped=false”。

于 2014-01-23T13:12:16.600 回答