开发者

Android: Accessing a function within a thread and getting the result?

In my android service, 开发者_运维技巧I'm starting a new thread like this

    thread = new Thread(new Runnable() {
        public void run() {
            new myClassName(getApplicationContext());
        }
    });
    thread.start();

The class is ran in a separate thread because it would otherwise block the service thread (causes timeout). That code is in the "onCreate" method, and is alive the whole time whilst the service is alive (unless it crashes, of course).

Now, I need to have a function in "myClassName" which returns a String. I've not actually created that function yet, but I'm trying to test it with a function which simply returns "hello", so let's call that function "helloFn".

    public String helloFn(String name) {
        return "hello "+name;
    }

How can I call "helloFn" which is inside the thread I just created, from my service? Thanks!


Create a new class which implements Runnable, and call helloFn(name) on the instance of that class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜