2013-08-25 3 views
0

나는 다음과 같은 코드가 있습니다내 안드로이드 서비스가 null 인 이유는 무엇입니까?

public void refreshWidget() { 
    doBindService(); 
    mBoundService.loadState(); 
    doUnbindService(); 
} 


private static ServiceConnection mAppWidgetServiceConnection = new ServiceConnection() { 

    @Override 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     // This is called when the connection with the service has been 
     // established, giving us the service object we can use to 
     // interact with the service. Because we have bound to a explicit 
     // service that we know is running in our own process, we can 
     // cast its IBinder to a concrete class and directly access it. 
     mBoundService = ((AppWidgetService.LocalBinder) service) 
       .getService(); 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName className) { 
     // This is called when the connection with the service has been 
     // unexpectedly disconnected -- that is, its process crashed. 
     // Because it is running in our same process, we should never 
     // see this happen. 
     mBoundService = null; 
    } 
}; 

static void doBindService() { 
    // Establish a connection with the service. We use an explicit 
    // class name because we want a specific service implementation that 
    // we know will be running in our own process (and thus won't be 
    // supporting component replacement by other applications). 
    if (!mIsBound) { 
     // mContext.startService(new Intent(mContext, 
     // AppWidgetService.class)); 
     mContext.bindService(new Intent(mContext, 
       AppWidgetService.class), 
       mAppWidgetServiceConnection, Context.BIND_AUTO_CREATE); 
     mIsBound = true; 
    } 
} 

가 어떻게이 줄 mBoundService.loadState();에서 그 mBoundService==null이 될 수 있습니까?

무엇이 누락 되었습니까?

답변

0

mBoundService.loadState();를 호출 할 수 없습니다. 이후 doBindService();은 서비스가 현재 제한되지 않았기 때문에 즉시 적용됩니다.

call mBoundService.loadState(); 콜백 후 onServiceConnected.

+0

onServiceConnected'mBoundService.startService (새로운 의도 (AppWidgetService.APPWIDGET_ACTION_CMD_REFRESH))'를 호출하려고합니다. 어떻게 서비스의'public void onStart (Intent aintent, int aStartId)'가 실행되지 않습니까? 'mBoundService.onStart (new Intent (AppWidgetService.APPWIDGET_ACTION_CMD_REFRESH), -1)를 호출 할 때만 -1; –

관련 문제