简体   繁体   中英

Method not executing?

I am running a 20 second timer in the background of my app using a service. I want the service to call my GameOver class when it is done with it's timer. 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.

To do this from a service, you need to run the Toast code on the main thread. 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. Very simple.
You create the CountDownTimer, but never call .start() on the object.

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.

facepalm

ALWAYS keep service declarations inside of tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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