开发者

How to use local Service instance to call methods outside of onServiceConnected() that are declared in ILocalService.java interface

Regarding Local Service Sample I have succeeded in defining methods declared in my ILocalService.java, but I don't know how to call these methods outside of an activity's onServiceConnected(). I can call only them from within onServiceConnected() which does not seem to be of much use. Am I limited to that? Here is my ILocalService.java:

/*********************************************************************开发者_C百科*****************************
 * Filename: ILocalService.java
 * Project name: Local Service Sample
 * Application name: Local Service
 * Description: This file contains an example interface for LocalService
 **************************************************************************************************/

package com.marie.localservicesample;

public interface ILocalService {

    public LocalService getService();

    public int getStatusCode();
}

My assumption is that I should be able to call methods declared in my ILocalService.java. These calls should not be limited to from within onServiceConnected() by an activity that is bound to my LocalService.java. How would I do this?


If you will use the service reference outside the serviceConnected method. Mostly it will throw nullpointerexception.

Make a button in your activity and try to use service reference in its onClick. Click the button at least waiting 10 seconds. It will work fine.

This indicates that service takes time to bind to the local reference variable. ServiceConnected method is only used to tell that service got connected and now it is usable.

In order to use properly service outside the serviceConnected method. You can maintain a boolean flag in the serviceConnected method.

On the basis of flag value use service anywhere you want

This is as far as i know.

Hope it helps :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜