我正在尝试使用尽可能少的代码从我的 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 之类的东西(由于某种未知原因,强制在此设备上关闭)。