package com.example.f1_telephone;
package com.example.f1_telephone; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.PhoneLookup; import android.database.Cursor; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.content.ContentResolver; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; public class MainActivity extends Activity implements OnItemClickListener{ private ListView listView; private TelephoneAdapter adapter; private List<TelephoneBean>list=new ArrayList<TelephoneBean>(); private TelephoneBean bean; Intent phoneIntent; String str; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView) findViewById(R.id.listView); //得到ContentResolver对象 ContentResolver cr = getContentResolver(); //取得电话本中开始一项的光标 Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); //向下移动光标 while(cursor.moveToNext()) { //取得联系人名字 int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); String contact = cursor.getString(nameFieldColumnIndex); //取得电话号码 String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null); while(phone.moveToNext()) { String PhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); bean=new TelephoneBean(contact, PhoneNumber); list.add(bean); } } adapter=new TelephoneAdapter(this, list); listView.setAdapter(adapter); cursor.close(); listView.setOnItemClickListener(this); } private void send1(String number){ Uri uri = Uri.parse("smsto:" + number); Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri); startActivity(sendIntent); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { str=list.get(arg2).getNumber(); phoneIntent = new Intent("android.intent.action.CALL",Uri.parse("tel:" + str)); AlertDialog dialog=new AlertDialog.Builder(this).setTitle("请选择服务项目").setItems(new String[]{"拨打电话","发送短信"}, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(which==0){ startActivity(phoneIntent); } if(which==1){ send1(str); } } }).setNegativeButton("取消", null).show(); } } package com.example.f1_telephone; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class TelephoneAdapter extends BaseAdapter{ private Context context; private List<TelephoneBean>list=new ArrayList<TelephoneBean>(); public TelephoneAdapter(Context context,List<TelephoneBean>list) { this.context=context; this.list=list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int arg0) { return list.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { Holde holde=new Holde(); if(arg1==null){ arg1=LayoutInflater.from(context).inflate(R.layout.list_telephone, null); holde.tvname=(TextView) arg1.findViewById(R.id.tv_name); holde.tvnumber=(TextView) arg1.findViewById(R.id.tv_number); arg1.setTag(holde); } else{ holde=(Holde) arg1.getTag(); } TelephoneBean telephoneBean=list.get(arg0); holde.tvname.setText(telephoneBean.getName()); holde.tvnumber.setText(telephoneBean.getNumber()); return arg1; } private class Holde{ public TextView tvname; public TextView tvnumber; } } package com.example.f1_telephone; public class TelephoneBean { public String name; public String number; public TelephoneBean(String name,String number) { this.name=name; this.number=number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } }
- 粉丝: 2
- 资源: 2
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助