将短信发送到已存储在电话簿中的所选联系人[recepient]

时间:2013-04-01 06:55:33

标签: android

我正在创建一个应用程序,其中我正在访问电话簿然后选择一个联系人向该特定收件人发送短信,但每当我尝试发送一条短信获取错误消息我在代码的Catch块中给出了以防失败:“短信失败,请稍后再试!”“

注意:我也在真实设备上试过这个,并在那里遇到同样的问题......

Contact.java ::

  public interface Contact {

public int getId();

public String getName();

public List<RawContact> getRawContacts();

}

BirthdayEditor.java ::

public class BirthdayEditor extends Activity implements OnItemClickListener {
private static final int CREATE_BIRTHDAY = 0;
private static final int EDIT_BIRTHDAY = 1;
private static final int DELETE_BIRTHDAY = 2;
private ImageButton saveGreeting;

public static final String CONTACT_ID = "contact_id";
private static final int NO_CONTACT_ID = Integer.MIN_VALUE;

private TextView name;
private ListView list;
private Database db;
private Contact contact;

ImageButton buttonSend;
EditText textSMS;

private Map<String, AuthenticatorDescription> map = new LinkedHashMap<String, AuthenticatorDescription>();

@Override
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());
    final 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();               

    buttonSend = (ImageButton) findViewById(R.id.buttonSend);
    textSMS = (EditText) findViewById(R.id.editTextSMS);
    buttonSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

          String recepient = contact.getName().toString();
              Log.d(LOG_TAG, "onClick(" + recepient + ")");
          String sms = textSMS.getText().toString();

          try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(recepient, 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();
          }

        }
    });
}

Logcat说:

04-01 14:12:29.211: D/BirthdayEditor(950): ButtonSend onClick RECEPIENT Stephen
04-01 14:12:29.211: D/BirthdayEditor(950): ButtonSend onClick MESSAGE Congrats
04-01 14:12:29.251: W/System.err(950): java.lang.NullPointerException
04-01 14:12:29.261: W/System.err(950):  at android.os.Parcel.readException(Parcel.java:1431)
04-01 14:12:29.261: W/System.err(950):  at android.os.Parcel.readException(Parcel.java:1379)
04-01 14:12:29.261: W/System.err(950):  at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:434)
04-01 14:12:29.261: W/System.err(950):  at android.telephony.SmsManager.sendTextMessage(SmsManager.java:87)
04-01 14:12:29.261: W/System.err(950):  at com.chr.tatu.sample.friendslist.contacts.BirthdayEditor$2.onClick(BirthdayEditor.java:110)
04-01 14:12:29.261: W/System.err(950):  at android.view.View.performClick(View.java:4202)
04-01 14:12:29.272: W/System.err(950):  at android.view.View$PerformClick.run(View.java:17340)
04-01 14:12:29.272: W/System.err(950):  at android.os.Handler.handleCallback(Handler.java:725)
04-01 14:12:29.272: W/System.err(950):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 14:12:29.272: W/System.err(950):  at android.os.Looper.loop(Looper.java:137)
04-01 14:12:29.272: W/System.err(950):  at android.app.ActivityThread.main(ActivityThread.java:5039)
04-01 14:12:29.272: W/System.err(950):  at java.lang.reflect.Method.invokeNative(Native Method)
04-01 14:12:29.272: W/System.err(950):  at java.lang.reflect.Method.invoke(Method.java:511)
04-01 14:12:29.272: W/System.err(950):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-01 14:12:29.272: W/System.err(950):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

的Manifest.xml:

我已经允许发送短信

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

1 个答案:

答案 0 :(得分:1)

@JimmyHill

你仍然得到了recepient的名字,因为你正在使用以下行的Contact.java类的 getName()方法:

    String recepient = contact.getName().toString();     

但要使用 getRawContacts()获取代号,您只需要将上面的行替换为下面一行,因为使用此代码可以在代码中从联系人列表中获取原始联系人,再次检查Contact.java:

    String recepient = contact.getRawContacts().toString();

我相信它会对你有用,如果出现任何问题,请告诉我......