Issue
I want to pass string from fragments to service. i tried by setvalue and bind but it works with activity not with startservice right? and what is "ServiceConnection" and by using ServiceConnection is it possible to pass string? here is my fragment code to start service.
I have change my code to this and it works perfect
Intent intent = new Intent(getActivity(), myPlayService.class);
Bundle b = new Bundle();
b.putString("link", "http://94.23.154/bbc");
intent.putExtras(b);
getActivity().startService(intent);
and in service i used
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
if(intent != null){
Bundle bundle = intent.getExtras();
link = bundle.getString("link");
}
Solution
You can pass string from Fragment to Service via Intent and using the putExtra()
method:
Intent intent = new Intent(getActivity(), myPlayService.class));
intent.putExtra("string param 1", "String for the Service");
getActivity().startService(intent);
In the Service you will retrieve the string in onStartCommand()
:
public int onStartCommand(Intent intent, int flags, int startId) {
String stringFromFragment = intent.getStringExtra("string param 1");
// TODO do something with the string
startPlayer();
return START_STICKY;
}
Answered By - petrsyn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.