开发者

ScheduledExecutorService doesnt start

I have some Problems with ScheduledExecutorService. I want to start the notification after some time. In this case its 10 seconds. But after 10 seconds it doesnt start. All functions beyond this working correctly.

Here is the code:

 ScheduledExecutorService scheduler =
                    Executors.newSingleThreadScheduledExecutor();
                 scheduler.schedule(new Runnable() { 
                   public void run() { 
                       Toast.makeText(BService.this, "It works", Toast.LENGTH_SHORT).show();

                        // Display a notification about us starting.  We put an icon in the status bar.
                        showNotification();
                    } 
                开发者_如何学Go  }, 10, TimeUnit.SECONDS);

Thanks for help Roa


I was having the same problem and I found out why. The run() is not being called on the UI thread and the Toast crashes that thread (but for some unknown reason it does not give anything on the LogCat).

But if you do:

task.schedule(new Runnable() {

    @Override
    public void run() {
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
        // your Toas goes in here
        }
    });
    }
}, DURATION, TimeUnit.MILLISECONDS);

it will work!


ScheduledExecutorService.schedule() is called correctly, so I guess that your run() does wrong for some reason. You may add a log at begin of run() to check it starts or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜