android 2.2 获取联系人,电话,并拨号

来源:互联网 发布:西安软件外包学院 编辑:程序博客网 时间:2024/05/16 18:13

主要功能有: 读取联系人姓名、号码,并lisetview 显示,获取listview数据,并发短信、或者拨号

package com.android.hello;

import android.app.Activity;
import android.content.Intent;  
import android.database.Cursor;
import android.graphics.Color;  
import android.net.Uri;  
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.util.Log;  
import android.view.View;  
import android.widget.AdapterView;  
import android.widget.LinearLayout;  
import android.widget.ListAdapter;  
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.provider.ContactsContract;

import java.util.ArrayList;  
import java.util.HashMap;
import android.widget.SimpleAdapter;

@SuppressWarnings("deprecation")
public class hello extends Activity {
/** Called when the activity is first created. */
// @SuppressWarnings("deprecation")
// @Override
//  
private static final String TAG="App";  
ListView listView;  
ListAdapter adapter;  
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
// setContentView(R.layout.main);  
LinearLayout linearLayout=new LinearLayout(this);  
linearLayout.setOrientation(LinearLayout.VERTICAL);  
linearLayout.setBackgroundColor(Color.BLACK);  
LinearLayout.LayoutParams param=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);  

listView=new ListView(this);  
listView.setBackgroundColor(Color.BLACK);  

linearLayout.addView(listView,param);  

this.setContentView(linearLayout);   


//生成动态数组,加入数据
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();       
ArrayList<HashMap<String, Object>> listItemRead = new ArrayList<HashMap<String, Object>>();    
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);     
while (cursor.moveToNext())   
{    
HashMap<String, Object> map = new HashMap<String, Object>();
String phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
map.put("ItemTitle", phoneName);//电话姓名
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));  

if (hasPhone.compareTo("1") == 0)   
{  
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);       
while (phones.moveToNext())   
{     
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));      
String phoneTpye = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));      

map.put("ItemText", phoneNumber); // 多个号码如何处理

Log.d(TAG,"testNum="+ phoneNumber + "type:"+phoneTpye);
}       
phones.close();      
}      
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);  
while (emails.moveToNext())   
{                   
String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));  
String emailType = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));      

                Log.d(TAG,"testNum="+ emailAddress + "type:"+emailType);
}      
emails.close();

listItem.add(map);
}

//生成适配器的Item和动态数组对应的元素  
SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源   
android.R.layout.simple_list_item_2,//ListItem的XML实现  
//动态数组与ImageItem对应的子项          
new String[] {"ItemTitle", "ItemText"},   
//ImageItem的XML文件里面的一个ImageView,两个TextView ID  
new int[] {android.R.id.text1,android.R.id.text2}  
);             

listView.setAdapter(listItemAdapter);  
cursor.close(); 

//listView.setEmptyView(findViewById(R.id.empty));  

listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){  

public void onItemSelected(AdapterView<?> arg0, View arg1,  
int arg2, long arg3) {  
// TODO Auto-generated method stub  
//openToast("滚动到:"+arg0.getSelectedItemId());  
//短信发送  
setTitle("选择"+arg2+"项目");
openToast("选择"+arg0.getSelectedItemId()+"项目");
RelativeLayout lr = (RelativeLayout) arg1;
TextView mText = (TextView) lr.getChildAt(1);
openToast(mText.getText().toString());

    String number = mText.getText().toString();
Log.d(TAG, "number=" + number);
// 判断电话号码的有效性
if (PhoneNumberUtils.isGlobalPhoneNumber(number)) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri
.parse("smsto://" + number));
intent.putExtra("sms_body", "The SMS text");
startActivity(intent);             
}
}  

public void onNothingSelected(AdapterView<?> arg0) {  
// TODO Auto-generated method stub  

}  

}); 

listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){  

   public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
// openToast("Click"+Integer.toString(position+1)+"项目");
RelativeLayout lr = (RelativeLayout) arg1;
TextView mText = (TextView) lr.getChildAt(1);
openToast(mText.getText().toString());

    String number = mText.getText().toString();
Log.d(TAG, "number=" + number);
// 判断电话号码的有效性
if (PhoneNumberUtils.isGlobalPhoneNumber(number)) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri
.parse("tel://" + number));
startActivity(intent);
}
}
});
}

private void openToast(String str){  
Toast.makeText(this,str,Toast.LENGTH_SHORT).show();  
}  

原创粉丝点击