APK安装过程中的服务注册

礼帽哈雷

最近我一直在搞弄 Android 内部结构。为了更好地理解 APK 的安装方式,我决定制作一个测试 APK 并在 root ADB shell 的帮助下手动安装它:

  • 我创建了一个应用程序目录/data/app/com.example.myapp-y6zjC4JYhJKOph-sl-G5QA==并将 APK 复制到那里(如base.apk)。目录后缀是从另一台设备上的“正确”安装复制的,即使我认为它可能是任何东西,因为它是 base64 编码的随机字符串
  • 我创建目录/data/data/com.example.myapp及其子目录cachecode_cache
  • 我修改packages.xmlpackages.list在其中添加了一个关于我的应用程序的条目(同样,从另一个“正确的”安装中复制)。

重新启动设备后,我可以使用 来验证应用程序是否已安装adb shell pm list packages | grep myapp,它会找到我的包。我可以使用adb shell am start -n com.example.myapp/.MainActivity.

但是,当我想在我的应用程序中实现一个服务(从 自动启动MainActivity.onStart)时,该服务不会启动并在日志中打印一条错误消息:

ActivityManager: Unable to start service Intent { cmp=com.example.myapp/.MyService } U=0: not found

该服务不显示在dumpsys

# adb shell dumpsys activity service com.example.myapp
No services match: com.example.myapp

我的调用方式startService如下:

Log.e("MYAPP", "About to start the service"); // This is printed to the log
Intent intent = new Intent(getApplicationContext(), MyService.class);
startService(intent);

我曾试图改变getApplicationContext()thisIntent争论,但它并没有帮助。该服务已在 AndroidManifest.xml 中正确注册:

<application ... >
 ...
    <activity android:name="com.example.myapp.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name="com.example.myapp.MyService" android:enabled="true"></service>
</application>

同样,我尝试更改清单中的一些内容:更改com.example.myapp.MyService.MyService、删除enabled标志并使用一个行标记(以 结尾/>)而不是<service>...</service>. 这些都没有帮助。

我的问题是:

  • 为什么系统看不到我的服务?
  • 在 APK 安装过程中,有没有办法“注册”这些服务?我需要修改一些其他的配置文件吗?
礼帽哈雷

没关系,我在问这个问题一天后发现了一个解决方案。

如果您遇到过这样的问题,出于某种原因,您需要像这样启动服务:

Intent intent = new Intent(getApplicationContext(), MyService.class);
intent.setClassName("com.example.myapp", "com.example.myapp.MyService"); // add this line
startService(intent);

我不知道为什么会发生这种情况,意图似乎相同。当我intent.toString()在调用之前和之后打印到日志时intent.setClassName,两行都如下所示:

Intent { cmp=com.example.myapp/.MyService }

如果有人知道这种行为的原因,请与我分享,因为我不知道为什么会发生这种情况。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章