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. How could I run a Uri based Intent in a Tab Page?

Sorry, currently a TabHost can only run activities that are part of your own
.apk (actually only those with the same uid, but in most cases that is the
same thing).

Dianne Hackborn
Android framework engineer

answer them.


2. autoLink text from app widget


I have an app widget with a TextView in it. I set its android:autoLink
property to "web|map" so that the links can be clicked to launch them in

The links become clickable alright, but when I click on them an exception is
thrown while opening the browser Activity.

E/AndroidRuntime( 5923): android.util.AndroidRuntimeException: Calling
startActivity() from outside of an Activity  context requires the
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you
E/AndroidRuntime( 5923):        at

E/AndroidRuntime( 5923):        at

E/AndroidRuntime( 5923):        at

E/AndroidRuntime( 5923):        at

E/AndroidRuntime( 5923):        at

This shows as "The process android.process.acore has stopped
unexpectedly..." on screen.

Since I am not launching the Browser activity in my code, I don't know where
I can mention the FLAG_ACTIVITY_NEW_TASK flag.

Any ideas?

Thanks in advance.



3. How do you display many points on a map?

4. How to get past "waiting for for device /dev/block/sdb2" on eee PC 1000HE?

5. ubuntu brainstorm

6. How I can attach other drawable image on drawable image?
