1

我正在尝试创建一个运行后台线程的简单应用程序。目的是向我的卵石手表发送一些数据,例如手机电池状态等。

我看这里: https ://developer.android.com/training/run-background-service/create-service.html

写了一些代码(我在这里跳过了“导入”):

public class MyWatch extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void sendMessage(View view) {
            Intent serviceIntent = new Intent(this, MyWatchService.class);
            serviceIntent.setData(Uri.parse("http://google.com/"));
            startService(serviceIntent);
    }
}
class MyWatchService extends IntentService {
    public MyWatchService() {
        super("MyWatchService");
    }
    @Override
    protected void onHandleIntent(Intent workIntent) {
        String localUrlString = workIntent.getDataString();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="ru.ozlabs.mywatchapp"
      android:versionCode="1"
      android:versionName="1.0"
      android:debuggable="true"
>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
    <activity android:name="MyWatch"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".MyWatchService"
        android:process="another_thread"
        android:exported="false"/>

编译(“ant -q debug”),安装在手机上(adb install ...),就是Moto G lollipop;当我运行该应用程序时,它崩溃了。如果我删除 startService(),它不会。我尝试通过 jdb 查看发生了什么,但我只看到这个:

Exception occurred: java.lang.RuntimeException (uncaught)"thread=main", com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(), line=907 bci=33

和“看所有”打印像这样的回溯(最后两个重复10次):

main[1] where all
main:
  [1] com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:907)
  [2] com.android.internal.os.ZygoteInit.main (ZygoteInit.java:696)
Signal Catcher:
Current thread isn't suspended.
FinalizerDaemon:
  [1] java.lang.Object.wait (native method)
  [2] java.lang.Object.wait (Object.java:422)
  [3] java.lang.ref.ReferenceQueue.remove (ReferenceQueue.java:101)
  [4] java.lang.ref.ReferenceQueue.remove (ReferenceQueue.java:72)
  [5] java.lang.Daemons$FinalizerDaemon.run (Daemons.java:173)
  [6] java.lang.Thread.run (Thread.java:818)
ReferenceQueueDaemon:
  [1] java.lang.Object.wait (native method)
  [2] java.lang.Daemons$ReferenceQueueDaemon.run (Daemons.java:133)
  [3] java.lang.Thread.run (Thread.java:818)

问题是 - 调试此类问题的技术是什么?代码中有什么明显的错误吗?

4

0 回答 0