Android:打電話和發短信:由姓名獲取到電話

在手機聯繫人中:由姓名查找到電話號碼(根據模糊“漢字”查找/姓名查找/電話查找到光標cursor,然後通過光標獲取該組的信息:姓名,聯繫電話,頭像等)
由光標獲取信息時:
1、光標首先指到某個位置
cursor.move(offset);
cursor.moveToPosition(position);
cursor.moveToFirst();
2、獲取多少個元組,>0才進行之後的步驟。
cursor.getCount()
3、獲取姓名…..
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
4、別忘加權限哈:

<!--打電話和發信息的-->
<uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
String[] projection =
                        { ContactsContract.PhoneLookup.DISPLAY_NAME,
                                ContactsContract.CommonDataKinds.Phone.NUMBER };
                        Cursor cursor = this
                                .getContentResolver()
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        projection, // Which columns to return.
                                        // 這是模糊查詢
                                        ContactsContract.PhoneLookup.DISPLAY_NAME
                                                + " LIKE"
                                                + " '%"
                                                + call_name
                                                + "%'",
                                        // WHERE clause.
                                        // (如果你根據具體的條件來查詢也可以這樣寫:)
                                        // ContactsContract.CommonDataKinds.Phone.NUMBER
                                        // +
                                        // " = '"
                                        // + number + "'", // WHERE clause.
                                        // // 這是根據電話號碼來查詢
                                        // ContactsContract.Contacts.DISPLAY_NAME
                                        // + " = '"
                                        // + call_name + "'", // WHERE
                                        // clause. //
                                        // 這是根據姓名來查詢
                                        null, // WHERE clause value
                                                // substitution
                                        null); // Sort order.
                        if (cursor == null)
                        {
                            // 找不到該人
                            Log.i("TAG", "getPeople null");

                        } else
                        {
                            Log.i("TAG", "getPeople cursor.getCount() = "
                                    + cursor.getCount());
                            if (cursor.getCount() > 0)
                            {
                                // 找到該人
                                cursor.moveToFirst();
                                callName = cursor
                                        .getString(cursor
                                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                                callNumber = cursor
                                        .getString(cursor
                                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            } 

打電話:

//Intent.ACTION_CALL:直接撥打:撥打按鈕已經按下
//Intent.ACTION_VIEW:聯繫人列表
// 打電話(啓動的頁面是未撥打:撥打按鈕還未按下)
startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + callNumber)));

發短信:

SmsManager smsManager = SmsManager.getDefault();
// 發送短信
//mNumber號碼,message短信內容
                if (message.length() > 70)
                {
                    List<String> contents = smsManager.divideMessage(message);
                    for (String sms : contents)
                    {
                        smsManager.sendTextMessage(mNumber, null, sms, null,
                                null);
                    }
                } else
                {
                    smsManager.sendTextMessage(mNumber, null, message, null,
                            null);
                }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章