呼叫联系人,选择联系人发送短信,然后发送短信 [英] call contacts, choose contact to send sms to then send sms
问题描述
我期待调用用户的联系人列表。然后,他们会选择一个联系人发送短信到。我有一个很难理解如何发送短信时节省使用联系人。任何帮助将是AP preciated。这里是我的code所以下面为止。
包com.example.practiceapp;进口android.os.Bundle;
进口android.provider.ContactsContract;
进口android.app.Activity;
进口android.content.Intent;
进口android.view.Menu;
进口android.view.View;公共类扩展元{活动 @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_dollar);
} // @覆盖
//公共布尔onCreateOptionsMenu(菜单菜单){
//getMenuInflater().inflate(R.menu.activity_dollar,菜单);
//返回true;
//} 公共无效的联系人(查看视图){
意向意图=新意图(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
INT PICK_CONTACT = 0;
startActivityForResult(意向,PICK_CONTACT);}
}
据presents工作示例选择一个电子邮件地址。
关键code,使用联系人选取器获得接触式ID后,是这样的:
//一切电子邮件查询
光标= getContentResolver()查询(
Email.CONTENT_URI,空,
Email.CONTACT_ID +=?,
新的String [] {ID},NULL);
其中, ID
是接触式ID从联系人选择器返回(在教程解释)。拿到手机的数据,您将需要此更改为:
//的一切电话查询
光标= getContentResolver()查询(
Phone.CONTENT_URI,空,
Phone.CONTACT_ID +=?,
新的String [] {ID},NULL);
请注意,联系人可以有多个电话号码,所以你必须建立一个第二个对话框,选择一个特定的电话号码。
的ArrayList<串GT; PHONENUMBERS =新的ArrayList<串GT;();
字符串名称=;
串号=;
INT nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
INT numberIdx = cursor.getColumnIndex(Phone.DATA);
//获取所有电话号码的人
如果(cursor.getCount()大于0){
cursor.moveToFirst();
做{
名称= cursor.getString(nameIdx); //从游标得到名称
数= cursor.getString(numberIdx); //从游标获取电话号码
phoneNumbers.add(数);
Log.v(DEBUG_TAG,得到的名字:+姓名+电话:
+号);
}而(cursor.moveToNext());
showPhoneNumberSelectionDialog(姓名,PHONENUMBERS);
}其他{
Log.w(DEBUG_TAG,无结果);
}
(或者你可以调整光标从需要上述code读)
私人无效showPhoneNumberSelectionDialog(字符串名称,
最终的ArrayList<串GT;电话号码) {
1.建立一个AlertDialog来接从PHONENUMBERS(标题with'name')的数字;
2.发送短信到该号码
}
构建警告对话框并不难,但涉及改造的ArrayList成String数组。见<一href=\"http://stackoverflow.com/questions/5374311/convert-arrayliststring-to-string/%22Convert%20ArrayList%3CString%3E%20to%20String%20%5B%5D%22\">Convert ArrayList中为String []
发送短信是相当容易的。有很多的例子在网络上。
这SO Q&安培; A(如何在Android 2.0读接触)也是很好的背景,但最终我没有使用任何code如此。
I'm looking to call the users contacts list. They will then choose a contact to send an sms to. I'm having a hard time understanding how to save the contact for use when sending the sms. Any help would be appreciated. Here is my code so far below.
package com.example.practiceapp;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class Dollar extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dollar);
}
//@Override
//public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.activity_dollar, menu);
//return true;
//}
public void contacts(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
int PICK_CONTACT=0;
startActivityForResult(intent, PICK_CONTACT);
}
}
I used Android Essentials: Using the Contact Picker
It presents a working example for selecting an email address.
The key code, after using the contact picker to get a contact ID, is this:
// query for everything email
cursor = getContentResolver().query(
Email.CONTENT_URI, null,
Email.CONTACT_ID + "=?",
new String[]{id}, null);
Where id
is the contact ID returned from the Contacts picker (as explained in the tutorial). To get the phone data, you will need to change this to:
// query for everything phone
cursor = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + "=?",
new String[]{id}, null);
Be aware that a contact can have multiple phone numbers, so you will have to build a second dialog to select a particular phone number.
ArrayList<String> phoneNumbers = new ArrayList<String>();
String name = "";
String number = "";
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
int numberIdx = cursor.getColumnIndex(Phone.DATA);
// Get all phone numbers for the person
if (cursor.getCount() > 0) {
cursor.moveToFirst();
do {
name = cursor.getString(nameIdx); // get the name from the cursor
number = cursor.getString(numberIdx); // get the phone number from the cursor
phoneNumbers.add(number);
Log.v(DEBUG_TAG, "Got name: " + name + " phone: "
+ number);
} while (cursor.moveToNext());
showPhoneNumberSelectionDialog(name,phoneNumbers);
} else {
Log.w(DEBUG_TAG, "No results");
}
(or you can adapt the cursor reading from the above code as needed)
private void showPhoneNumberSelectionDialog(String name,
final ArrayList<String> phoneNumbers) {
1. Build an AlertDialog to pick a number from phoneNumbers (with'name' in the title);
2. Send an SMS to the number
}
Building the alert dialog is not hard, but involves transforming the ArrayList into a String array. See Convert ArrayList to String []
Sending the SMS is reasonably easy. There are plenty of examples on the web.
This SO Q&A (How to read contacts on Android 2.0) is also good background, but in the end I didn't use any code from it.
这篇关于呼叫联系人,选择联系人发送短信,然后发送短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!