Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data from Activity to Service using an Intent

How do I get data within an Android Service that was passed from an invoking Activity?

like image 686
GobiasKoffi Avatar asked Jul 20 '10 18:07

GobiasKoffi


People also ask

How do you pass data between activities using intent?

This example demonstrates how do I pass data between activities in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How pass data from activity to services in Android?

Explanation. Using putExtra() method, we can send the data. While using it, we need to call setResult() method in services. We can also store data in a common database and access it on services as well as in Activity.


2 Answers

For a precise answer to this question on "How to send data via intent from an Activity to Service", Is that you have to override the onStartCommand() method which is where you receive the intent object:

When you create a Service you should override the onStartCommand() method so if you closely look at the signature below, this is where you receive the intent object which is passed to it:

  public int onStartCommand(Intent intent, int flags, int startId) 

So from an activity you will create the intent object to start service and then you place your data inside the intent object for example you want to pass a UserID from Activity to Service:

 Intent serviceIntent = new Intent(YourService.class.getName())  serviceIntent.putExtra("UserID", "123456");  context.startService(serviceIntent); 

When the service is started its onStartCommand() method will be called so in this method you can retrieve the value (UserID) from the intent object for example

public int onStartCommand (Intent intent, int flags, int startId) {     String userID = intent.getStringExtra("UserID");     return START_STICKY; } 

Note: the above answer specifies to get an Intent with getIntent() method which is not correct in context of a service

like image 164
user_CC Avatar answered Sep 24 '22 11:09

user_CC


First Context (can be Activity/Service etc)

For Service, you need to override onStartCommand there you have direct access to intent:

Override public int onStartCommand(Intent intent, int flags, int startId) { 

You have a few options:

1) Use the Bundle from the Intent:

Intent mIntent = new Intent(this, Example.class); Bundle extras = mIntent.getExtras(); extras.putString(key, value);   

2) Create a new Bundle

Intent mIntent = new Intent(this, Example.class); Bundle mBundle = new Bundle(); mBundle.extras.putString(key, value); mIntent.putExtras(mBundle); 

3) Use the putExtra() shortcut method of the Intent

Intent mIntent = new Intent(this, Example.class); mIntent.putExtra(key, value); 

New Context (can be Activity/Service etc)

Intent myIntent = getIntent(); // this getter is just for example purpose, can differ if (myIntent !=null && myIntent.getExtras()!=null)      String value = myIntent.getExtras().getString(key); } 

NOTE: Bundles have "get" and "put" methods for all the primitive types, Parcelables, and Serializables. I just used Strings for demonstrational purposes.

like image 21
Pentium10 Avatar answered Sep 23 '22 11:09

Pentium10