开发者

Can not call getApplicationContext() inside a thread

I am developing an application on Android Froyo system, everything is fine, except that when I create a service, and the service will spawn several thread. In one of the thread, I want to stop the service. Apparently stopService() is out of scope in the thread class开发者_C百科, and so is the getApplicationContext() calls. So inside a thread, can I get the current context and how?

Thanks


Declare your own context

private Context mContext;

and use insted of getApplicationContext()


You can try MyService.this.stopSelf(). Or if that doesn't work, you can store a reference to your service when it's created. In your oncreate you can do Context ctx = this. And then in your thread ctx.stopSelf()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜