I want to create a class which would take a phone number, send a SMS to that number and wait for answer from that specific number. Originally I planned something like this:
public class SmartSocket extends BroadcastReceiver {
private String mPhoneNum;
private SmartSocketMessageListener mListener;
public SmartSocket(String phoneNum, SmartSocketMessageListener l) {
mPhoneNum = phoneNum;
mListener = l;
}
void requestStatus() {
SmsManager smsMan = SmsManager.getDefault();
smsMan.sendTextMessage(mPhoneNum, null, "STATUS", null, null);
}
public void onReceive(Context context, Intent intent) {
//.... process received Intent, extract phone number
if (phoneNumFrom == mPhoneNum) {
//... we've got the answer, process it
mListener.statusMessageReceived(messageText);
}
}
}
But this is not possible because Android needs an empty constructor for that class and creates a new instance every time a message is received. Is there a way around it or some pattern to achieve what I need? I want to avoid application level variables which might probably solve this problem.