Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 971813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:01:23+00:00 2026-05-16T03:01:23+00:00

I have an Activity communicating with and directly accessing a background Service’s data members

  • 0

I have an Activity communicating with and directly accessing a background Service’s data members through get functions. The problem is that I can’t seem to be able to access the actual data members. For instance, I try and get an ArrayList with a few items in it but I just receive an empty ArrayList.

This code is almost directly from the local service tutorial in the Android Service class doc.

Here’s my service: 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;
}

And here’s my bound activity:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:01:24+00:00Added an answer on May 16, 2026 at 3:01 am

    Thought I would answer this just in case there were other people in the same situation.

    I fixed this by using the

    startService(Intent intent);
    

    method before binding to it. If only bindService(…) is used to start a service, the service will be destroyed once there are no bound activities or other services attached to it. Using startService(…) allows the service to exist without any bindings.

    This is exactly what was happening in my program; I was calling unbindService(…) in the onDestroy() method of my Activity and then binding again using bindService(…) in the onStart() method on the next Activity. Hence, each Activity I was launching was starting a stopping a brand new service.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Activity that uses a ListView to simply display a list of
I have an Activity that contains a FrameLayout and two views. The first is
I have Activity for displaying search results. It extends ListActivity. I need to show
I have a two layouts files in my app. Also I have Activity extends
Lets say, i have the following table | start | end | activity +---------------------+---------------------+---------
I have 3 activity in my application. My first activity (Main) has 2 button
I have an activity with its layout set to an XML file. the root
I have an Activity in which user can update a specific information clicking in
I have an activity which handles configuration changes. But now i have to change
I'm stuck with a service, which can't be found using it's explicit component name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.