Skip to content Skip to sidebar Skip to footer

How To Pass A Value In Services?

I am making a media player ,who can run in background. i have to send a string uri in this service. but i cant send the uri value from my activity through bundle.Services class doe

Solution 1:

You should use IntentService http://developer.android.com/reference/android/app/IntentService.html.

Implement onHandleIntent(Intent intent) which will be called each time you start service with the startService(intent). You can pass your data to service through this intent.

Solution 2:

If you are trying to send a string or String-array from one Activity to another this can be done in the Intent.

In ClassA:

Intent intent = new Intent(this, ClassB); tring[] myStrings = new String[] {"test", "test2"}; intent.putExtra("strings", myStrings); startActivity(intent);

In ClassB:

public void onCreate() { Intent intent = getIntent(); String[] myStrings = intent.getStringArrayExtra("strings"); }

Post a Comment for "How To Pass A Value In Services?"