Alberto Martínez
Alberto Martínez

Reputation: 251

How to setExtras from Activity to Service?

So I need to send a btDevice defined in one Activity to a Service in the params so I can get it. Here is the code of the service:

public class Servicio extends JobService implements ServiceConnection {

private static BluetoothDevice btDevice;
public TemperaturaFragment temperaturaFragment;
private MetaWearBoard mwBoard;

@Override
public boolean onStartJob(JobParameters params) {
    mJobHandler.sendMessage( Message.obtain( mJobHandler, 1, params ) );
    params.getExtras(btDevice = getParcelableExtra(EXTRA_BT_DEVICE));
    getApplicationContext().bindService(new Intent(this, BtleService.class), (ServiceConnection) this, BIND_AUTO_CREATE);
    return true;
}



@Override
public boolean onStopJob(JobParameters params) {

    return false;
}
private Handler mJobHandler = new Handler(new Handler.Callback() {

    @Override
    public boolean handleMessage( Message msg ) {

        Toast.makeText( getApplicationContext(),
                "JobService task running", Toast.LENGTH_SHORT )
                .show();
        jobFinished( (JobParameters) msg.obj, false );
        return true;
    }

} );

And here is the object that I want to send that is defined in the activity:

btDevice= getIntent().getParcelableExtra(EXTRA_BT_DEVICE);

i want to use builder.setExtras(the object here) and then in the service getExtras(get the object) but the set extras say it is a BluetoothDevice and needs a bundle. Any idea? Thanks for the help.

I put this and doesn't work, because the data is just read, not sent

Intent serviceIntent = new Intent(Servicio.class.getName())
                    serviceIntent.putExtra(1, getIntent().getParcelableExtra(EXTRA_BT_DEVICE));
                    getApplicationContext().startService(serviceIntent);

Upvotes: 1

Views: 333

Answers (1)

Chetan Joshi
Chetan Joshi

Reputation: 5711

Because setExtras() method of Intent takes input value as Bundle object and BluetoothDevice object is not Bundle class object.

you can pass primitive data type , serialized ,Parcelable and ArrayList through Bundle and Intent .

Bound Services

A bound service is the server in a client-server interface. It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

Bound Service Example In Android

And you will find how to call service class method from Activity

Define an interface your Service will use to communicate events:

EDIT

you can send data in Service through Intent like below code:

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

And you can retrieve that Intent object in callback method of Service Class called :

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;
}

EDIT 2

you can also send BluetoothDevice object directly to Intent like below code because its BluetoothDevice class implements Parcelable interface.

Intent serviceIntent = new Intent(YourService.class.getName())
Bundle bundle = new Bundle();
bundle.putParcelable("btDevice",bluetoothDevice);
serviceIntent.putExtras(bundle);
context.startService(serviceIntent);

And you can retrieve its like below in service class: in onStartCommand method

BluetoothDevice btDevice = (BluetoothDevice ) bundle.getParcelable("btDevice");

Upvotes: 1

Related Questions