我需要这样的情况帮助:
我必须能够结束的活动,再次启动它,并从新的活动的实例停止IntentService。
公共类的MyService扩展IntentService { 公众为MyService() { 超(则将MyService); } 公共则将MyService(字符串名称){ 超(名); } @覆盖 保护无效onHandleIntent(意向意图){ Log.d(服务,开始); 而(真) { SystemClock.sleep(1000); Log.d(服务,嘀); } } @覆盖 公共无效的onDestroy(){ Log.d(服务,结束); super.onDestroy(); }
...
意向意图=新意图(getApplicationContext(),MyService.class);startService(意向);我打过电话 stopService(),但它不工作。是否有任何解决方案是如何做到这一点?先谢谢了。
解决方案服务做好,而周期和睡眠的一些工作有一段时间了。
恕我直言,这是一个不恰当的使用 IntentService 的。请创建一个普通的服务,用自己的后台线程,你自己管理。
有没有什么解决方案是如何做到这一点?
创建一个普通的服务,用您自己管理自己的后台线程。例如,你可以使用 ScheduledExecutorService的,而不是你的睡眠()循环,用关机()或 shutdownNow时()在服务的的onDestroy()。
I need help with this situation:
I must be able to end activity, start it again and stop IntentService from new "instance" of activity.
public class MyService extends IntentService { public MyService() { super("MyService"); } public MyService(String name) { super(name); } @Override protected void onHandleIntent(Intent intent) { Log.d("SERVICE", "start"); while(true) { SystemClock.sleep(1000); Log.d("SERVICE", "tick"); } } @Override public void onDestroy() { Log.d("SERVICE", "end"); super.onDestroy(); }...
Intent intent = new Intent(getApplicationContext(), MyService.class); startService(intent);I tried calling stopService(), but it's not working. Is there any solution how to do this? Thanks in advance.
解决方案Service do some job in while cycle and sleep for some time.
IMHO, this is an inappropriate use of IntentService. Please create a regular Service, with your own background thread that you manage yourself.
Is there any solution how to do this?
Create a regular Service, with your own background thread that you manage yourself. For example, you could use a ScheduledExecutorService instead of your sleep() loop, using shutdown() or shutdownNow() in the service's onDestroy().
更多推荐
从活动停止IntentService
发布评论