2010-07-12 61 views
0

我有一個活動通過獲取函數與後臺服務的數據成員進行通信並直接訪問它。問題是,我似乎無法訪問實際的數據成員。例如,我嘗試獲得一個ArrayList,其中包含一些項目,但我只收到一個空的ArrayList。後臺服務的訪問數據成員

此代碼幾乎直接來自Android Service類doc中的本地服務教程。

這裏是我的服務:CommunicationService

public static final String BROADCAST_ALARM = "Alarm"; 
private Intent mAlarmBroadcast = new Intent(BROADCAST_ALARM); 

private AlarmList mAlarms; 

class TempTask extends AsyncTask<String, Void, Void> { 

    private int id = 0; 

    @Override 
    protected Void doInBackground(String... params) { 
     while (true) { 
      mAlarms.addAlarm(++id, "Description", "Label"); 


      sendBroadcast(mAlarmBroadcast); 

      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 

      } 
     } 
    } 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 

    mAlarms = new AlarmList(); 
    new TempTask().execute("test"); 
} 

public AlarmList getAlarmList() { 
    return mAlarms; 
} 

這是我的束縛活動:

private CommunicationService mComService = null; 

private IconicAdapter mListAdapter; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 

    setContentView(R.layout.alarm_list); 

    mListAdapter = new IconicAdapter(this, R.layout.row, R.id.label); 
    setListAdapter(mListAdapter); 

    // Attempt to bind with the service. 
    bindService(new Intent(this, CommunicationService.class), mOnService, BIND_AUTO_CREATE); 
} 

/* 
* Receives broadcasts from the bound service. 
*/ 
private BroadcastReceiver receiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (action != null) { 
      if (action.equals(CommunicationService.BROADCAST_ALARM)) { 
       AlarmList alarmList = mComService.getAlarmList(); 

       ArrayList<Alarm> alarms = alarmList.getAllAlarms(); 
       /* THIS IS WHERE THE PROBLEM EXISTS. 
       * alarms is an ArrayList of size 0, where as if you break 
       * over the actual data member in the service class, it is 
       * full of elements. 
       */ 
       mListAdapter.addAll(alarms); 
      } 
     } 
    } 
}; 

private ServiceConnection mOnService = new ServiceConnection() { 
    @Override 
    public void onServiceConnected(ComponentName name, IBinder binder) { 
     mComService = ((CommunicationService.LocalBinder) binder).getService(); 

     appendAlarms(); 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName name) { 
     // This is called when the connection with the service has been 
     // unexpectedly disconnected -- that is, its process crashed. 
     mComService = null; 
    } 
}; 

回答

1

以爲我會回答這個問題,以防萬一出現了同樣的情況其他人。

我通過結合到其之前使用

startService(Intent intent); 

方法解決了這個問題。如果僅使用bindService(...)來啓動服務,那麼一旦沒有綁定活動或其他服務附加到該服務,該服務就會被銷燬。使用startService(...)允許服務在沒有任何綁定的情況下存在。

這正是我的程序中發生的情況;我在Activity的onDestroy()方法中調用unbindService(...),然後在下一個Activity的onStart()方法中使用bindService(...)再次綁定。因此,我開展的每項活動都開始停止一項全新的服務。

+0

作爲一個方面說明,要停止已使用startService(Intent intent)開始的服務,您必須確保它未綁定到任何ServiceConnection,然後對其調用stopService(Intent intent)。 – mdupls 2010-08-05 14:22:59