
当然,我已经设置了一个简单的服务来做到这一点.我设置了调试消息,告诉我服务何时启动,何时更新(每分钟)以及何时关闭.我还有一条消息告诉我何时在runOnUiThread()方法中更新值.除runOnUiThread()中的消息外,我的所有消息都会激活.有什么我做错了(当然有)?我需要改变什么?
码:
@OverrIDepublic voID handleMessage(Message message) { try { if (!serviceStarted) { serviceStarted = true; serviceTest = true; while (serviceStarted) { new MainActivity().runOnUiThread(new Runnable() { public voID run() { OvervIEwFragment.refresh(getApplicationContext()); System.out.println("yay"); } }); Thread.sleep(((1 /* minutes */) * 60 * 1000)); System.out.println("Updated values through service."); } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); e.printstacktrace(); } stopSelf(message.arg1);}解决方法 So there’s no need to do that,unless you’re creating a Thread insIDe
of it
Gabe Sechan的回答是正确的.
但是,如果您使用单独的线程,而不是遵循代码:
new MainActivity().runOnUiThread(new Runnable() { public voID run() { OvervIEwFragment.refresh(getApplicationContext()); System.out.println("yay"); }}); 试试,这段代码:
new Handler(Looper.getMainLooper()).post(new Runnable() { public voID run() { OvervIEwFragment.refresh(getApplicationContext()); System.out.println("yay"); }}); 按照Android docs
总结Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise).
以上是内存溢出为你收集整理的android – 为什么runOnUiThread()在服务中不起作用?全部内容,希望文章能够帮你解决android – 为什么runOnUiThread()在服务中不起作用?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)