Given the code:
public class CommandSequence {
public CommandSequence() {
}
public void startCommunications(View v) {
Bundle dataout1 = new Bundle();
dataout1.putInt("ACTION", Communications.ACTION_LOAD_COMMAND_ONLY);
dataout1.putInt("PORT", Commands.MESSAGE_TYPE_SMC);
dataout1.putInt("COMMAND", Commands.SMC_RESETEVENTSTATUS);
((MainActivity) v.getContext()).sendMessageToBackgroundCommunicationsService(
Communications.MESSAGE_LOAD_COMMAND,
dataout1);
}
}
I must cast ‘sendMessageToBackgroundCommunicationsService()’ with the calling activity context, which is ‘MainActivity’.
Is it possible to pass a parameter that will allow me to cast the method call at runtime, so that this method can be called from any activity class?
Why not create a base activity class that all your activies inherit and then cast to this instead when you need to make a call:
[EDIT] In fact to make your code a bit better you could pass the activity into your method so there is no dependency on your class needing to know another class.