Use of intents

by Shekhar » Wed, 17 Feb 2010 01:45:59 GMT

Sponsored Links
 Hi Folks,

I am little confused with the use of intents used for sending data to
activity from service.

In my application I have to have startactivity from the service and
have to pass data ,so that activity can utilize the data
while launching.For this i have written the following code

 Intent intent = new Intent(Service.this,Activity.class);
 intent.putExtra("data", data);

I assume that the data is passed to the activity and can be parsed on
the oncreate function of the activity.

Now the service running in the background has to pass data to the
activity continously for UI updates.For this I have
written the following code

Intent intent = new Intent(Service.this, Activity.class);
intent.putExtra("Data", data);
sendBroadcast(intent,null); (Do I need to broadcast the intent???)

In activity I have done following things:-
Implemented broadcast reciever:

private BroadcastReceiver mBroadcastReceiver = new
public void onReceive(Context context, Intent intent)
if (Intent.ACTION_ATTACH_DATA.equals(intent.getAction()))
Bundle extra = intent.getExtras();
float Data[] = extra.getFloatArray("Data");

Also registered the broadcast reciever in the OnStart function as

public void onStart()
IntentFilter filter = new IntentFilter();
registerReceiver(mBroadcastReceiver, null);

Is this the right way of meeting my requirements.



Use of intents

by TreKing » Wed, 17 Feb 2010 01:57:28 GMT


You have a background service and an Activity running at the same time? Why?
If you're trying to background work that will effect the UI of your main
activity, just do it in a separate thread or use AsyncTask and post the
changes to the UI. It doesn't sound like you need a Service in this case,
unless I'm not fully getting the purpose of your app.

TreKing - Chicago transit tracking app for Android-powered devices 


Sponsored Links

Use of intents

by Mark Murphy » Wed, 17 Feb 2010 02:26:27 GMT


It's certainly one way. An alternative is to have the activity register
some sort of callback or listener object with the service, that the
service uses when events need to be propagated to the activity. That
will be more efficient and more private than your implementation, but it
involves a bit more code, usually.

Mark Murphy (a Commons Guy)  | 

Android App Developer Books: 


Use of intents

by Shekhar » Fri, 26 Feb 2010 16:30:55 GMT

 Hi Mark,

Could you please provide some pointers on how to implement listener
object for the service.



Other Threads

1. Notificationbar on top of a fullscreen application.

There have been a lot of changes in cupcake to how the status bar is
hidden/shown, so it may be fixed there.

Dianne Hackborn
Android framework engineer

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.


2. No callback when list item is touched.

I have a ListView that is populated with a custom adapter.  When an
item is clicked with the trackball, it works fine, i.e. I can catch
the event and move on, though when I touch an item with my finger
there is no callback from the listener.  The item does respond to the
touch, by turning orange for that moment.

Here is how I've setup the listener.  This is within an Activity, not
a ListActivity.

AdapterView.OnItemClickListener() {

                                        public void onItemClick(AdapterView<?> 
arg0, View arg1,
                                                        int position, long id) {


What am I missing? Is there another listener I need to configure?

3. OnGestureListener vs ContextMenu, do they play together?

4. TabHost and Scrollbars

5. need some collection help

6. Bouncing ListView?

7. emulator versus real target...