Sending an object to a service through Intent without binding

后端 未结 3 1297
悲哀的现实
悲哀的现实 2021-02-13 05:07

Is is possible to send an object to an Android Service through an Intent without actually binding to the service? Or maybe another way for the Service to access Objects...

3条回答
  •  迷失自我
    2021-02-13 05:58

    If you don't want to implement Parcelable and your object is serializable

    use this

    In the sender Activiy

    Intent intent = new Intent(activity, MyActivity.class);
    
    Bundle bundle = new Bundle();
    bundle.putSerializable("my object", myObject);
    
    intent.putExtras(bundle);
    
    startActivity(intent);
    

    In the receiver:

    myObject = (MyObject) getIntent().getExtras().getSerializable("my object");
    

    Works fine for me try it. But the object must be serializable :)

提交回复
热议问题