Bala u1
Bala u1

Reputation: 174

Android- How to send data from activity to service?

In my application I'm trying to send data from my MainActivity.class to a service called bgservice.class. This is my following code:

MainActivity.class:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Intent serviceIntent = new Intent(bgservice.class.getName());
  serviceIntent.putExtra("UserID", "123456");
  this.startService(serviceIntent);
}

bgservice.class:

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

  String userID = intent.getStringExtra("userID");
  System.out.println(userID);
  Toast.makeText(this,userID, Toast.LENGTH_LONG).show();
  return START_STICKY;

}

but I'm not getting the data in the service class. these are the following error I get:

02-25 09:05:41.166: E/AndroidRuntime(2633):

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.microapple.googleplace/com.microapple.googleplace. MainActivity}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.microapple.googleplace.bgservice (has extras) }

AndroidManifest.xml:

     ...
    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >



        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:enabled="true" android:name=".bgservice" />
</application>
....

Upvotes: 12

Views: 26664

Answers (2)

Apurva
Apurva

Reputation: 7911

Intent serviceIntent = new Intent(YourActivity.this, bgservice.class);
        serviceIntent.putExtra("UserID", "123456");
        this.startService(serviceIntent);

And in your service,

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

    String userID = intent.getStringExtra("UserID");

    //do something

    return START_STICKY;

}

This must work.

Upvotes: 7

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

Here:

 Intent serviceIntent = new Intent(bgservice.class.getName());

Passing String to Intent constructor for creating Intent to start Service. Intent constructor takes String as a Action name which we have added in AndroidManifest.xml.

<service android:enabled="true" android:name=".bgservice">
<intent-filter >
        <action android:name="com.microapple.googleplace.bgservice" />
    </intent-filter>
</service>

Now use com.microapple.googleplace.bgservice as action name to create Intent:

      Intent serviceIntent = new Intent("com.microapple.googleplace.bgservice");
      serviceIntent.putExtra("UserID", "123456");
      this.startService(serviceIntent);

OR

Use Intent constrictor which takes Context as first parameter and component name which we want to start as second parameter :

  Intent serviceIntent = new Intent(this,bgservice.class);
  serviceIntent.putExtra("UserID", "123456");
  this.startService(serviceIntent);

And also use same key which using to add data in Intent currently adding value with UserID key but trying to get value using userID key

Upvotes: 3

Related Questions