Reputation: 1302
Currently, I am working on an extensible sensing and data processing framework for Android device. This framework will enables a wide range of data signals (e.g., location, wifi, battery, accelerometer) via Android mobile device.
I consider that these data signals are publishers, which publish data. Classes that extends Activity (public class MainActivity extends Activity
) are subscribers.
I believe that an ideal way of implementing this functionality is to implement a small publish/subscribe middleware in between publishers and subscribers.
Can you please suggest -- How can I implement publish/subscribe middleware on Android device ? For me, the main issue is -- how the MainActivity
class can register itself for getting events and how various data signals notify to many regitered MainActivity
class? Please note this middleware does not need advance features like communication over network.
Upvotes: 2
Views: 2763
Reputation: 5626
Think about an example this way:
In this situation, when the fragment publishes anything new, it does that through a call to its method and passes back the arguments as needed.
try{
((YourInterFaceListetener) activity).someMethod(arguments);
}catch(ClassCastException cce){
Log.d(cce.getMessage());
}
Doing this creates that easy communication between the other parts of the app with an activity and you can extend it by simply implementing required interfaces.
Good luck!
Upvotes: 0
Reputation: 2485
Somebody did it for you http://square.github.io/otto/. Use this framework or check the sourcecode.
Upvotes: 5