大佬教程收集整理的这篇文章主要介绍了android – handler.postDelayed在IntentService的onHandleIntent方法中不起作用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
final Handler handler = new Handler(); LOG.d("delay"); handler.postDelayed(new Runnable() { @Override public void run() { LOG.d("notify!"); //calling some methods here } },2000);
“延迟”确实显示在日志中,但根本不显示.并且在run()中调用的方法也根本不被调用.任何人都可以帮助解释为什么会发生这种情况,我做错了吗?
具有此代码的类扩展了Intentservice,这会是一个问题吗?
============================
更新:
我将此代码放在扩展Intentservice的类中.我发现它唯一有用的地方是构造函数.但我需要把它放在onHandleIntent方法中.所以我检查了onHandleIntent的文档,它说:
所以基于我得到的结果,我觉得我不能在“工作线程”中使用postDelayed.但是,任何人都可以解释这一点,比如为什么这不适用于工作线程?提前致谢.
HandlerThread handlerThread = new HandlerThread("BACkground-thread"); handlerThread.start(); final Handler handler = new Handler(handlerThread.getLooper()); handler.postDelayed(new Runnable() { @Override public void run() { LOG.d("notify!"); // call some methods here // make sure to finish the thread to avoid leaking memory handlerThread.quitSafely(); } },2000);
或者您可以使用Thread.sleep(long millis).
try { Thread.sleep(2000); // call some methods here } catch (InterruptedException E) { e.printStackTrace(); }
如果要停止休眠线程,请使用yourThread.interrupt();
以上是大佬教程为你收集整理的android – handler.postDelayed在IntentService的onHandleIntent方法中不起作用全部内容,希望文章能够帮你解决android – handler.postDelayed在IntentService的onHandleIntent方法中不起作用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。