0

私はクラス Main_Thread を持っています:

public class Main_Thread extends Thread {
...
}

次のように、 Android サービスを Main_Thread に開始するにはどうすればよいですか。

startService(new Intent(this, Main_Service.class));
4

2 に答える 2

1

このコードで:

  startService(new Intent(this, Main_Service.class));

thisApplicationContextへの参照を持つコンテキストであるアクティビティへの参照です。

その行を機能させるには、スレッドへの ApplicationContext 参照を与えて呼び出す必要があります。

  startService(new Intent(context, Main_Service.class));
于 2012-12-19T12:05:29.070 に答える
0

コンテキスト参照がある場合:

startService(new Intent(context, Main_Service.class));

そうでなければ、Activity と同じクラスでスレッドを宣言する場合:

startService(new Intent(YourActivity.this, Main_Service.class));

それ以外の場合、それが実際のアクティビティであることがわかっている場合は、次のことができます。

startService(new Intent(ActualActivity.class, Main_Service.class));
于 2012-12-19T12:08:34.327 に答える