访问后台服务的数据成员

时间:2010-07-12 18:31:28

标签: android

我有一个Activity通过get函数与之通信并直接访问后台服务的数据成员。问题是我似乎无法访问实际的数据成员。例如,我尝试获取一个包含几个项目的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 个答案:

答案 0 :(得分:2)

以为如果有其他人处于同样的情况,我会回答这个问题。

我使用

解决了这个问题
startService(Intent intent);

绑定到它之前的方法。如果仅使用bindService(...)来启动服务,则一旦没有绑定的活动或附加的其他服务,该服务将被销毁。使用startService(...)允许服务存在而不需要任何绑定。

这正是我的计划中发生的事情;我在我的Activity的onDestroy()方法中调用unbindService(...),然后在下一个Activity的onStart()方法中使用bindService(...)再次绑定。因此,我推出的每项活动都开始停止一项全新的服务。