Pregunta

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.

¿Fue útil?

Solución

Somebody did it for you http://square.github.io/otto/. Use this framework or check the sourcecode.

Otros consejos

Think about an example this way:

  • SomeFragmentA - this fragment defines an interface with methods in it
  • SomeFragmentB - this also defines an interface with methods in it
  • MainActivity - this activity registers for updates from the above fragments
  • Now, the activity will be listening for events by simply implementing the interfaces from those two fragments and doing whatever it wants.

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!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top