通过安卓设备发送短信失败

3

我正在开发一个应用程序,尝试向收件人发送短信,但每当我点击发送时,会收到消息:“短信发送失败,请稍后再试!”

无论我使用模拟器还是Android设备....

如下面的屏幕截图所示,我正在尝试向Pratik发送消息,他保存在我的电话簿联系人中,但每当我尝试向Pratik发送消息时,无法发送消息给Pratik。

请参见下面的屏幕截图,如您所见,我正在尝试向Rahul发送消息...

Manifest.xml:

<uses-permission android:name="android.permission.SEND_SMS" />

请查看以下代码:
    private TextView name;
private ListView list;
private Database db;
private Contact contact;
ImageButton buttonSend;
EditText textSMS;

     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editor);

    // bind GUI components
    this.name = (TextView) findViewById(R.id.editor_name);
    this.list = (ListView) findViewById(R.id.editor_list);

    // check if contact id is valid
    this.db = new Database(getContentResolver());
    int contactId = getIntent().getIntExtra(CONTACT_ID, NO_CONTACT_ID);
    this.contact = this.db.getContact(contactId);
    if (this.contact == null) {
        finish();
    }
    this.name.setText(this.contact.getName());

    // pre-load information about all account types
    AuthenticatorDescription[] authTypes = AccountManager.get(this).getAuthenticatorTypes();
    for (AuthenticatorDescription authDesc : authTypes) {
        this.map.put(authDesc.type, authDesc);
    }

    // bind list events
    this.list.setOnItemClickListener(this);
    this.list.setOnCreateContextMenuListener(this);

    // create the GUI
    updateView();


    saveGreeting = (ImageButton) findViewById(R.id.greeting);
    saveGreeting.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            customGreeting(v);
        }
    });
    buttonSend = (ImageButton) findViewById(R.id.buttonSend);
    textSMS = (EditText) findViewById(R.id.editTextSMS);
    buttonSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

          String sms = textSMS.getText().toString();

          try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(CONTACT_ID, null, sms, null, null);
            Toast.makeText(getApplicationContext(), "SMS Sent!",
                        Toast.LENGTH_LONG).show();
          } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                "SMS faild, please try again later!",
                Toast.LENGTH_LONG).show();
            e.printStackTrace();
          }

        }
    });
}

接收者 = name.getText() 这个应该怎么工作? - njzk2
1个回答

2

不要使用收件人姓名发送消息,使用手机号发送消息。

从联系人列表中获取联系人号码,并在sendTextMessage函数中使用该联系人号码代替名称。


那么,伙计,无法向选定的朋友发送消息吗? - ASMUIRTI
可以实现,但您需要设置数字而不是名称。您需要将名称设置为视图以向用户显示详细信息,但需要在发送消息的函数中传递数字。 - Chirag
首先,请从联系人列表中获取联系电话并将其存储在一个字符串中,然后将该字符串传递到发送消息函数中。 - Chirag

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接