0

我想创建一个类,它需要一个电话号码,向该号码发送短信并等待该特定号码的回答。最初我计划这样的事情:

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 需要该类的空构造函数,并在每次收到消息时创建一个新实例。有没有办法解决它或某种模式来实现我所需要的?我想避免可能解决此问题的应用程序级变量。

4

0 回答 0