简体   繁体   English

方法不执行?

[英]Method not executing?

I am running a 20 second timer in the background of my app using a service. 我正在使用服务在我的应用程序后台运行20秒计时器。 I want the service to call my GameOver class when it is done with it's timer. 我希望该服务在计时器完成后调用我的GameOver类。 By the way, the user is constantly switching activities while the timer is running, but when it finishes, the user has to be taken to the game over screen no matter what. 顺便说一句,当计时器运行时,用户一直在切换活动,但是当计时器结束时,无论如何,都必须通过屏幕将用户带到游戏中。 I have put a toast in the service, but that doesn't show up. 我已经为该服务敬酒了,但是没有出现。

Here is my service: 这是我的服务:

在此处输入图片说明

Here is my Main Menu where I call the service: 这是我调用服务的主菜单:

在此处输入图片说明

Here is the manifest where I show the service: 这是我向您展示服务的清单:

在此处输入图片说明

Please let me know as to why the service or the timer aren't running. 请让我知道为什么服务或计时器未运行。 Thank you so much for all of your help, I really appreciate it! 非常感谢您的所有帮助,非常感谢! If you need any more code, just let me know and I will show you. 如果您需要更多代码,请告诉我,我会告诉您。 Thanks! 谢谢!

:-) :-)

Your toast is not showing because a Service can't directly manipulate the UI, such as showing toasts. 由于服务无法直接操纵UI(例如显示吐司),因此未显示吐司。

To do this from a service, you need to run the Toast code on the main thread. 要通过服务执行此操作,您需要在主线程上运行Toast代码。 This can be done as such: 可以这样进行:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(TwentySeconds.this.getApplicationContext(), "Started!", Toast.LENGTH_SHORT).show();
    }
});

Your CountDownTimer isn't working because you never start it. 您的CountDownTimer无法正常工作,因为您从不启动它。 Very simple. 很简单。
You create the CountDownTimer, but never call .start() on the object. 您创建CountDownTimer,但不要在对象上调用.start()。

cdt.start();

After a few hours on this issue, I figured out my pathetic error: 在这个问题上几个小时后,我发现了可悲的错误:

I had the service declaration OUTSIDE of the '< application>' end tag. 我有'<application>'结束标记的服务声明OUTSIDE。

facepalm 面容

ALWAYS keep service declarations inside of tag 始终将服务声明保留在标记内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM