2013-08-25 2 views
0

У меня есть следующий код:Почему моя ограниченная служба андроида равна нулю?

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==null в этой строке mBoundService.loadState();?

Что мне не хватает?

ответ

0

Вы не можете позвонить по телефону mBoundService.loadState(); после doBindService(); немедленно, потому что сервис не ограничен в это время.

звонок mBoundService.loadState(); после обратного вызова onServiceConnected.

+0

в 'onServiceConnected' Я пытаюсь вызвать' mBoundService.startService (новый Intent (AppWidgetService.APPWIDGET_ACTION_CMD_REFRESH)) '. Почему при запуске службы public void onStart (Intent aIntent, int aStartId) 'не выполняется? только когда я вызываю 'mBoundService.onStart (новый Intent (AppWidgetService.APPWIDGET_ACTION_CMD_REFRESH), -1);' –

Смежные вопросы