0

I want to parcelable a Handler object to send it by a Bundle from one Activity to a service in order to get some info from this service.

Right now, to test it it's a simple message. Here's the code in the Activity:

private MyHandler mHandlerSharing = new MyHandler() {
    public void handleMessage(Message msg) {
        // this line in the Activity is never reached when debugging
        String data = msg.getData().getString("data");
        Toast.makeText(mContext, data, Toast.LENGTH_SHORT).show();
    }
};

// in some function
mSecureSharing.putExtra(Constants.HANDLER, (Parcelable) mHandlerSharing);

Then, in the Service onStartCommandMetehod I do the following:

    MyHandler myHandler = (MyHandler) intent.getExtras().getParcelable(Constants.HANDLER);
    Message msgObj = myHandler.obtainMessage();
    Bundle b = new Bundle();
    b.putString("data", "SecureSharing running");
    msgObj.setData(b);
    myHandler.sendMessage(msgObj);

The class MyHandler is the following:

import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;

public class MyHandler extends Handler implements Parcelable{
private int mData;

public MyHandler(){
    super();
}

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel out, int flags) {
    out.writeInt(mData);
}

public static final Parcelable.Creator<MyHandler> CREATOR = new Parcelable.Creator<MyHandler>() {
    public MyHandler createFromParcel(Parcel in) {
        return new MyHandler(in);
    }

    public MyHandler[] newArray(int size) {
        return new MyHandler[size];
    }
};

private MyHandler(Parcel in) {
    mData = in.readInt();
}

}

The service receives the custom handler but never but and call sendMessage method from the class Handler, but the Activity never receives the message...

For MyHandler class, I basically used the code from the android developer site and add the Handler inheritance as well as a constructor.

What can be wrong?

Thanks in advance!

Fernando
  • 751
  • 2
  • 13
  • 27

1 Answers1

-1

I assume you want your activity to communicate with service these can be your choices:

  1. Use ResultReceiver an example could be found here
  2. Use LocalBroadcastReceiver example could be found here
  3. You can use Binder to bind your service to activity example could be found here
  4. If you want to use RPC then you should go with Messanger an example could be found here and here
  5. For RPC you can also use AIDL files example could be find here and of course on developer site
Shubhang Malviya
  • 1,525
  • 11
  • 17
  • Downvoted, this doesn't give an answer to the problem, just a laundry list of examples. – Kristopher Micinski Mar 21 '16 at 16:26
  • @KristopherMicinski I appreciate your taking time to explain why you downvoted. BUT Have you tried to understand the problem that the guy was having ? If not then I would request you to please read the first para of the question. In the Later part he has only elaborated what he did till now to achieve the solution around that problem. And Brother, trust me I have been involved in training groups & juniors for a while now, and could gather what kind of solution one is expecting. You could downvote and call your friends and downvote, what matters for me is finally the guy had his answer!! :) – Shubhang Malviya Apr 20 '16 at 18:24
  • I have "tried to understand the problem that the guy was having" and while I agree that a few of these could solve his problem, StackOverflow is a place for providing answers to questions in a concrete and targeted way, not merely providing pointers to things that might work. – Kristopher Micinski Apr 20 '16 at 20:37
  • @KristopherMicinski First of all, by reading your reply, I think you're somewhat hurt by my comment so please accept my apologies :) , further I really appreciate and welcome your suggestion about answering in targeted way. But still In this case do you really feel that I should solve the problem in code and not point in the right direction with the correct alternatives ? Thanks for your time :) – Shubhang Malviya Apr 22 '16 at 05:17
  • It also hurts those of us that come from Google to find an answer, only to have to press back and look for another link \*presses back\* – Kobato Mar 20 '21 at 03:15