怎么调用系统通讯录并向被选中联系人发送短信
生活随笔
收集整理的這篇文章主要介紹了
怎么调用系统通讯录并向被选中联系人发送短信
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
每做一個項目都會有收獲,前提是要在這個項目上付出努力的!
好吧,如今講一下:怎么通過調用系統通訊錄,當你點擊聯系人姓名時,跳轉到向其發送短信的頁面<收件人是被點中的聯系人,短信已自己主動編輯>。
看看圖片效果:
以下看一下具體代碼:
Uri result = data.getData();String phoneName = getPhoneContacts(result);Log.d("phone", "---------->phoneName=="+contactName);String smsContent="發給你一個時尚園APP的注冊邀請碼,他們家的東西非常有品。" +""+"\n"+strInviteCode+"(時尚園 APP下載地址http://t.so)";sendSMS(phoneName,smsContent);/*** 獲取聯系人手機號碼* @param contactId* @return*/ @SuppressWarnings("deprecation")private String getPhoneContacts(Uri contactId) {Cursor cursor = null;String phoneName = "";//聯系人姓名String phoneNum = "";//聯系人電話號碼String phoneID = "";//聯系人IDtry { // Uri uri = People.CONTENT_URI;cursor=getContentResolver().query(contactId, null, null, null, null);if (cursor.moveToNext()) {phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));phoneID=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));Log.d("phone", "phoneName&&phoneNumber=="+phoneName+"-----"+phoneID);Cursor c=getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID+"="+phoneID, null, null);while(c.moveToNext()){phoneNum=c.getString(c.getColumnIndex("data1"));Log.d("phone", "phoneNumber=="+phoneNum);}} else {Toast.makeText(this, "找不到該聯系人",Toast.LENGTH_LONG).show();}} catch (Exception e) {e.printStackTrace();} finally {if (cursor != null) {cursor.close();}}return phoneNum;}
/*** 發送短信* * @param smsBody*/private void sendSMS(String phoneNum, String smsBody) {Log.d("phone", "sendSMS(String phoneNum, String smsBody)=="+phoneNum);Uri smsToUri = Uri.parse("smsto:"+phoneNum);Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);intent.putExtra("sms_body", smsBody);startActivity(intent);// Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", phoneNum, null)); // mmsintent.putExtra("sms_body", smsBody); // startActivity(mmsintent);/* 建立SmsManager對象 */ // SmsManager smsManager = SmsManager.getDefault(); // smsManager.sendTextMessage(phoneNum, null, smsBody, null, null);}
轉載于:https://www.cnblogs.com/mfrbuaa/p/3800101.html
總結
以上是生活随笔為你收集整理的怎么调用系统通讯录并向被选中联系人发送短信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot 10:处理Jso
- 下一篇: 经典监督学习方法