2013-03-01 64 views
3

我正在写一个应用程序,我试图发送短信给Recepient,但每当我点击发送,获取消息: - 短信展示,请稍后再试!通过Android设备发送短信失败

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

就像你可以看到下面的屏幕截图,在这里,我试图将消息发送到PRATIK,这是保存在我的电话簿联系人,但每当我我试图消息PRATIK无法发送消息PRATIK

请参考下面的屏幕截图,就像你所看到的,我在这里要发送消息,拉胡尔...

的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(); 
      } 

     } 
    }); 
} 
+0

'recepient = name.getText()'这应该如何工作? – njzk2 2013-03-01 08:41:52

回答

2

不要使用收件人的姓名发送消息时,使用移动号码发送消息。

从联系人列表中获取联系人号码,并在sendTextMessage函数中使用该联系人号码而不是姓名。

+0

所以好友是不可能发送消息给选定的朋友? – ASMUIRTI 2013-03-01 07:29:02

+0

这是可能的,但你必须设置数字而不是名称。您必须为您的视图设置名称以向用户显示详细信息,但需要在传递消息的功能中传递号码。 – 2013-03-01 07:29:55

+0

首先请从联系人列表中获取联系人号码并将其存储在一个字符串中,并将该字符串传递给发送消息功能 – 2013-03-01 08:45:47