无法在拨号器中解析“#”符号

时间:2014-04-24 11:51:55

标签: java android

我正在制作定制拨号器,到目前为止一切都很完美。现在在测试时,我看到了"#"标志不正常。它显示在拨号器上,但在呼叫时,它消失了。例如,如果我拨* 121#,当通过股票拨号器进行呼叫时,它变为* 121。这是我活动的代码 -

public class DialPadActivity extends Activity {

private EditText numberField = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initializeButtonArrowClickListeners();
}

public void dialButtonClick(View v) {
    int buttonId = v.getId();
    putNumberToEditText(buttonId);
}

public void buttonPhone_click(View v) {
    if (numberField != null) {
        String phone = numberField.getText().toString();
        String uriString = "tel:" + phone;
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse(uriString));
        startActivity(intent);
    }
}

public void putNumberToEditText(int buttonId) {
    switch (buttonId) {
    case R.id.dial_button0:
        putNumber("0");
        break;
    case R.id.dial_button1:
        putNumber("1");
        break;
    case R.id.dial_button2:
        putNumber("2");
        break;
    case R.id.dial_button3:
        putNumber("3");
        break;
    case R.id.dial_button4:
        putNumber("4");
        break;
    case R.id.dial_button5:
        putNumber("5");
        break;
    case R.id.dial_button6:
        putNumber("6");
        break;
    case R.id.dial_button7:
        putNumber("7");
        break;
    case R.id.dial_button8:
        putNumber("8");
        break;
    case R.id.dial_button9:
        putNumber("9");
        break;
    case R.id.dial_button_s:
        putNumber("*");
        break;
    case R.id.dial_button_p:
        putNumber("#");
        break;
    }
}

public void putNumber(String number) {
    if (numberField == null) {
        numberField = (EditText) findViewById(R.id.phone_number_field);
    }
    numberField.append(number);
}

private void initializeButtonArrowClickListeners() {
    ImageButton buttonArrow = (ImageButton) findViewById(R.id.button_arrow);
    buttonArrow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (numberField != null && numberField.length() > 0) {
                String previousNumbers = numberField.getText().toString();
                String numbersMinusTheLast = previousNumbers.substring(0,
                        numberField.length() - 1);
                numberField.setText(numbersMinusTheLast);
            }
        }
    });
    buttonArrow.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (numberField != null) {
                numberField.setText("");
            }
            return false;
        }
    });
}

我在哪里做错了?

2 个答案:

答案 0 :(得分:4)

使用URLEncoder.encode(字符串,“UTF-8”);

例如:

String uriString = "tel:" + URLEncoder.encode(phone, "UTF-8");

答案 1 :(得分:1)

将#符号替换为%23。

即:*121%2311代替*121#11

            String uri = "tel:" + "*6133%235345";

            Intent intent;
            intent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
            startActivity(intent);

输出为:* 6133#5345