我想创建一个课程,该课程将接听电话号码,发送短信到该号码并等待该特定号码的答复。最初我计划这样的事情:
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);
}
}
}
但这是不可能的,因为Android需要该类的空构造函数,并在每次收到消息时创建一个新实例。有没有办法绕过它或某种模式来实现我的需要?我想避免可能解决这个问题的应用程序级变量。