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);
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity();

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);
intent.setAction(Intent.ACTION_ATTACH_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
BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if (Intent.ACTION_ATTACH_DATA.equals(intent.getAction()))
{
Bundle extra = intent.getExtras();
float Data[] = extra.getFloatArray("Data");
update(Data);
}
}
}

Also registered the broadcast reciever in the OnStart function as
below:-

public void onStart()
{
super.onStart();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_ATTACH_DATA);
registerReceiver(mBroadcastReceiver, null);
}

Is this the right way of meeting my requirements.

Thanks,
Shekhar

--



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
 http://sites.google.com/site/rezmobileapps/treking 

--


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)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Android App Developer Books:  http://commonsware.com/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.

Regards,
Shekhar







--



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
hack...@android.com

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

--~--~---------~--~----~------------~-------~--~----~

2. autoLink text from app widget

Hi,

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
browser.

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
want?
E/AndroidRuntime( 5923):        at
android.app.ApplicationContext.startActivity(ApplicationContext.java:627)

E/AndroidRuntime( 5923):        at
android.text.style.URLSpan.onClick(URLSpan.java:62)

E/AndroidRuntime( 5923):        at
android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:216)

E/AndroidRuntime( 5923):        at
android.widget.TextView.onTouchEvent(TextView.java:6315)

E/AndroidRuntime( 5923):        at
android.view.View.dispatchTouchEvent(View.java:3368)
....

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.

Jayesh

--~--~---------~--~----~------------~-------~--~----~

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?

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