繁体   English   中英

(EditText)Android联系人列表获取电话号码

[英](EditText)Android Contact list getting phone number

我是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();
            }

        }
    });

是否意味着要用设备上联系人中的匹配联系人姓名包装输入号码? 为此,您需要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#mychipsedittexthttps://github.com/kpbird/chips-edittext-library

// 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" />

这是一个简单的把戏。 我希望您能够轻松地将其修复为您的代码。 您可以在其setOnClickListener上放置一个按钮(SMS),可以使用此按钮。

        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();
            }

        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM