2013-03-01 59 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