以编程方式发送SMS时无效的DestinationAdress异常

时间:2015-11-19 13:33:08

标签: java android

用户应该在“edixxx”EditText中输入一个电话号码,我得到号码并尝试向其发送“msghoshharon”字符串,但是当我点击按钮时我得到这个例外:“无效的目的地地址”

有什么想法吗?我无法弄清楚这段代码有什么问题! :(

寻求帮助。

package com.sms.validation;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class acadmin extends Activity {


private SharedPreferences sphala;
public String enterd;

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

    final String msghoshharon="admin123456";


    sphala = getSharedPreferences("myPrefs",
            MODE_PRIVATE);
    final String number = (sphala.getString("number", ""));

    EditText text = (EditText)findViewById(R.id.edixxx);
    enterd = text.getText().toString();

    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            sendSMS(enterd, msghoshharon);
        }
    });

}

public void sendSMS(String enterd, String msghoshharon) {
    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(enterd, null, msghoshharon, null, null);
        Toast.makeText(getApplicationContext(), "has been sent",
                Toast.LENGTH_LONG).show();
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(), ex.getMessage().toString(),
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }
}
}

1 个答案:

答案 0 :(得分:0)

试试这个

    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.telephony.SmsManager;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class acadmin extends AppCompatActivity implements View.OnClickListener {


    private SharedPreferences sphala;
    public String enterd;

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

        final String msghoshharon="admin123456";


        sphala = getSharedPreferences("myPrefs",
                MODE_PRIVATE);
        final String number = (sphala.getString("number", ""));

        EditText text = (EditText)findViewById(R.id.edixxx);


        final Button button = (Button) findViewById(R.id.button);


    }

    public void sendSMS(String enterd, String msghoshharon) {
        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(enterd, null, msghoshharon, null, null);
            Toast.makeText(getApplicationContext(), "has been sent",
                    Toast.LENGTH_LONG).show();
        } catch (Exception ex) {
            Toast.makeText(getApplicationContext(), ex.getMessage().toString(),
                    Toast.LENGTH_LONG).show();
            ex.printStackTrace();
        }
    }

 @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
            enterd = text.getText().toString();
                sendSMS(enterd, msghoshharon);
                break;
        }
    }
    }