Android: Passing data between service and activity

后端 未结 2 747
一生所求
一生所求 2020-12-23 22:41

So I\'m getting really confused on how to do this whole thing and I was hoping someone could break it down for me a little bit.

I have a service that should always

2条回答
  •  爱一瞬间的悲伤
    2020-12-23 23:00

    1. If the Service and Activity classes are in the same package, I believe they should be able to access the same private storage space.

    2. I would put them in the same package since they are part of the same overall application structure.

    3. Bundle the data into an Intent's extras as Key-value pairs and start the activity.


    Intent intent = new Intent(this, SecondActivity.class);
    Bundle b = new Bundle();
    
    // see Bundle.putInt, etc.
    // Bundle.putSerializable for full Objects (careful there)
    b.putXXXXX("key", ITEM);  
    intent.putExtras(b);
    startActivity(intent);
    
    // -- later, in Activity
    Bundle b = this.getIntent().getExtras();
    int i = b.getInt("key");
    

提交回复
热议问题