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
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en