1、开始界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/et_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入联系人" /> <Button android:onClick="click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="联系人" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/et_number2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入联系人" /> <Button android:onClick="click2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="联系人2" /> </LinearLayout> </LinearLayout>
2、开启新的activity代码
1 package com.example.smssender; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.view.Menu; 7 import android.view.View; 8 import android.widget.EditText; 9 10 public class MainActivity extends Activity { 11 12 private EditText et_number; 13 private EditText et_number2; 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 et_number = (EditText) findViewById(R.id.et_number); 19 et_number2 = (EditText) findViewById(R.id.et_number2); 20 } 21 22 public void click(View view){ 23 Intent intent = new Intent(this, ContactActivity.class); 24 //startActivity(intent); 25 //请求码的作用是区别是谁发起的请求 26 startActivityForResult(intent, 1); 27 } 28 29 public void click2(View view){ 30 Intent intent = new Intent(this, ContactActivity.class); 31 //startActivity(intent); 32 //请求码的作用是区别是谁发起的请求 33 startActivityForResult(intent, 2); 34 } 35 36 37 @Override 38 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 39 // TODO Auto-generated method stub 40 super.onActivityResult(requestCode, resultCode, data); 41 if(data != null){ 42 String number = data.getStringExtra("number"); 43 if(requestCode == 1){ 44 et_number.setText(number); 45 }else{ 46 et_number2.setText(number); 47 } 48 } 49 } 50 51 }
3、获取联系人
1)清单文件
<uses-permission android:name="android.permission.READ_CONTACTS"/>//权限
2)通过内容提供者获取联系人
1 package com.example.smssender; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.content.ContentResolver; 7 import android.content.Context; 8 import android.database.Cursor; 9 import android.net.Uri; 10 11 public class ContactService { 12 public static List<contactInfo> getContactAll(Context context){ 13 List<contactInfo> infos = new ArrayList<contactInfo>(); 14 //通过内容提供者获取联系人 15 ContentResolver resolver = context.getContentResolver(); 16 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); 17 Uri dataUri = Uri.parse("content://com.android.contacts/data"); 18 Cursor cursor = resolver.query(uri, null, null, null, null); 19 while(cursor.moveToNext()){ 20 String id = cursor.getString(cursor.getColumnIndex("contact_id")); 21 Cursor datacursor = resolver.query(dataUri, null, "raw_contact_id=?", new String[]{id}, null); 22 contactInfo info = new contactInfo(); 23 while(datacursor.moveToNext()){ 24 String data1 = datacursor.getString(datacursor.getColumnIndex("data1")); 25 String mimetype = datacursor.getString(datacursor.getColumnIndex("mimetype")); 26 if("vnd.android.cursor.item/name".equals(mimetype)){ 27 info.setUsername(data1); 28 }else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){ 29 info.setNumber(data1); 30 } 31 } 32 33 infos.add(info); 34 35 } 36 return infos; 37 } 38 }
4、设置联系人进Listview
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ListView 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:id="@+id/lv_contact" 11 ></ListView> 12 13 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/et_username" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 /> 12 <TextView 13 android:id="@+id/et_number" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 /> 17 18 </LinearLayout>
java代码:
1 package com.example.smssender; 2 3 import java.util.List; 4 5 import android.app.Activity; 6 import android.content.ContentResolver; 7 import android.content.Intent; 8 import android.database.Cursor; 9 import android.net.Uri; 10 import android.os.Bundle; 11 import android.view.View; 12 import android.view.ViewGroup; 13 import android.widget.AdapterView; 14 import android.widget.AdapterView.OnItemClickListener; 15 import android.widget.BaseAdapter; 16 import android.widget.ListView; 17 import android.widget.TextView; 18 19 public class ContactActivity extends Activity { 20 21 private ListView lv_contact; 22 private List<contactInfo> infos = null; 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 26 // TODO Auto-generated method stub 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_contact); 29 30 infos = ContactService.getContactAll(this); 31 32 lv_contact = (ListView)findViewById(R.id.lv_contact); 33 lv_contact.setAdapter(new ContactAdapter()); 34 35 lv_contact.setOnItemClickListener(new OnItemClickListener() { 36 37 @Override 38 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 39 long arg3) { 40 // TODO Auto-generated method stub 41 contactInfo info = infos.get(arg2); 42 String number = info.getNumber(); 43 Intent data = new Intent(); 44 data.putExtra("number", number); 45 setResult(0, data); 46 //关闭当前activity,把数据传回给它的激活者 47 finish(); 48 49 } 50 51 }); 52 } 53 54 private class ContactAdapter extends BaseAdapter{ 55 56 @Override 57 public int getCount() { 58 // TODO Auto-generated method stub 59 return infos.size(); 60 } 61 62 @Override 63 public Object getItem(int arg0) { 64 // TODO Auto-generated method stub 65 return null; 66 } 67 68 @Override 69 public long getItemId(int arg0) { 70 // TODO Auto-generated method stub 71 return 0; 72 } 73 74 @Override 75 public View getView(int arg0, View arg1, ViewGroup arg2) { 76 // TODO Auto-generated method stub 77 contactInfo info = infos.get(arg0); 78 View view = View.inflate(ContactActivity.this, R.layout.contact_item, null); 79 TextView et_username = (TextView)view.findViewById(R.id.et_username); 80 et_username.setText(info.getUsername()); 81 82 TextView et_number = (TextView)view.findViewById(R.id.et_number); 83 et_number.setText(info.getNumber()); 84 85 return view; 86 } 87 88 } 89 90 }
时间: 2024-10-27 13:23:36