我是Android开发的新手。我用过 ;
我在EditText中键入数字,现在我想从联系人列表中获取联系号码。有人可以帮我解决这个问题吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText pnumber= (EditText) findViewById(R.id.editTextPnumber);
final EditText gsms= (EditText) findViewById(R.id.editTextSMS);
Button sendsms= (Button) findViewById(R.id.buttonSend);
sendsms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String gPhone= pnumber.getText().toString();
String gSMS= gsms.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(gPhone, null, gSMS, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent ! ", Toast.LENGTH_LONG).show();
finish();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), "PLS Enter Again ! ", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
答案 0 :(得分:0)
如果您想要从设备上的联系人中获取具有匹配联系人姓名的换行编号? 为此你想要的 1)ContactProvider http://developer.android.com/guide/topics/providers/contacts-provider.html 2)自动完成http://developer.android.com/guide/topics/ui/controls/text.html#AutoComplete 3)如果你不想要一个号码,你可以使用一些筹码。 (EditText,跨越)https://github.com/nichtemna/ChipsEditText#mychipsedittext,https://github.com/kpbird/chips-edittext-library
答案 1 :(得分:0)
// try this way here i gave sample or demo code you modify as per your requirement.
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="phone number"
android:layout_weight="1"/>
<ImageView
android:id="@+id/imgPickUpContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<EditText
android:id="@+id/edtMessage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="message"/>
<Button
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send"/>
</LinearLayout>
2.MyActivity
public class MyActivity extends Activity {
private EditText edtNumber;
private EditText edtMessage;
private Button btnSend;
private ImageView imgPickUpContact;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtNumber=(EditText)findViewById(R.id.edtNumber);
edtMessage=(EditText)findViewById(R.id.edtMessage);
btnSend=(Button)findViewById(R.id.btnSend);
imgPickUpContact=(ImageView)findViewById(R.id.imgPickUpContact);
imgPickUpContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent localIntent = new Intent("android.intent.action.PICK", ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(localIntent, 1);
}
});
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendSMS(edtNumber.getText().toString(), edtMessage.getText().toString());
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent paramIntent) {
super.onActivityResult(requestCode, resultCode, paramIntent);
if (resultCode == RESULT_OK) {
String str = getPhoneNumber(paramIntent.getData());
if (str.trim().length() > 0) {
edtNumber.setText(str);
edtNumber.setSelection(edtNumber.getText().length());
}
} else {
Toast.makeText(this,"Phone Number Not Founded ...",Toast.LENGTH_SHORT).show();
}
}
private String getPhoneNumber(Uri paramUri) {
String id = "";
String no="";
Cursor cursor = getContentResolver().query(paramUri, null, null, null, null);
while(cursor.moveToNext()){
id = cursor.getString(cursor.getColumnIndex("_id"));
if("1".equalsIgnoreCase(cursor.getString(cursor.getColumnIndex("has_phone_number")))){
Cursor cursorNo = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, "contact_id = " + id, null, null);
while (cursorNo.moveToNext()) {
if (cursorNo.getInt(cursorNo.getColumnIndex("data2")) == 2){
no = no.concat(cursorNo.getString(cursorNo.getColumnIndex("data1")));
break;
}
}
cursorNo.close();
}
}
cursor.close();
return no;
}
private void sendSMS(String paramString1, String paramString2) {
Intent localIntent = new Intent("android.intent.action.SENDTO", Uri.parse("smsto:" + paramString1));
localIntent.putExtra("sms_body", paramString2);
startActivity(localIntent);
}
}
3.please define this permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS" />
答案 2 :(得分:0)
这是一个简单的伎俩。我希望您能够轻松地将此修复到您的代码中。 您可以将一个按钮(SMS)放到其setOnClickListener中,您可以使用它。
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(),
"PLS Enter Again ! ", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});