android - 调试服务

标签 android debugging android-service android-manifest

我编写了一个带有远程接口(interface)的服务并将其安装在我的 PC 的 Eclipse AVD 上。我有一个客户端测试工具,它启动并调用服务中的方法。最初,我通过一个控制类和 Activity 安装了该服务,现在我已将其删除,因此该服务的 list 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myname.gridservice"
android:versionCode="1"
android:versionName="1.0">
<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:debuggable="true">
    <service
        android:enabled="true"
        android:debuggable="true"
        android:name="OverlayService">
        <intent-filter>
            <action android:name="com.myname.OverlayService.SERVICE"/>
            <action android:name="com.myname.gridservice.IRemoteInterface" />
        </intent-filter>
    </service>
 </application>  
</manifest>   

所以没有 Activity 标签。

当我从 Eclipse 中的调试图标启动它时,控制台告诉我它正在安装 apk(它是),但它没有显示为调试线程并且没有触发断点,尽管服务的行为是就客户看到的而言,还可以。如果我将服务标签包装在具有关联类的 Activity 标签中并启动它,那么我可以对其进行调试。

是否可以在不将服务包装在 Activity 中的情况下对其进行调试?

最佳答案

以下是您可以分四个步骤执行的操作:

第一个:在您的服务的第一个有趣的方法中(我在创建时使用):

/* (non-Javadoc)    
 * @see android.app.Service#onCreate()
 */
@Override
public void onCreate() {
    super.onCreate();
    //whatever else you have to to here...
    android.os.Debug.waitForDebugger();  // this line is key
}

第二:在waitForDebugger命令后的任意位置设置断点。

第三:通过 IDE 中的调试按钮启动应用程序(Eclipse/Android Studio/...)。 (您现在应该已经从 list 中删除了主要的启动 Activity )

最后:启动 adb 并运行命令启动服务:

  • cd $PLATFORM_TOOLS
  • adb shell
  • am startservice -n com.google.android.apps.gtalkservice/com.google.android.gtalkservice.service.GTalkService

关于android - 调试服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4008081/

相关文章:

Thrive 上的 Android 调试

javascript - ASP .net - 调试 javascript(快速断点问题)

安卓。使用服务拦截符合条件的短信

android - 带 ListView 的 Google Card Style 9-Patch - Android

javascript - 如何在用户运行我们的应用程序时获取设备详细信息?

javascript - PHP 中的 Node.js console.time() 等效吗?

android - 如何在 Android 中使用警报管理器启动服务?

android - 应用程序在后台运行时无法找到位置更新

android - ios 10 的 Ionic Cordova 相机问题

android - 其他包中的复合控件