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

该Android应用示例演示了如何读取设备联系人的姓名和电话号码,将其显示在ListView中,并实现点击联系人拨打电话的功能。通过查询ContactsContract库获取联系人数据,对结果进行遍历,将数据添加到ArrayList中,再用SimpleAdapter填充ListView。当用户在ListView中选择一项时,应用会检查并拨打选中电话号码。
摘要由CSDN通过智能技术生成

该demo是第一次基于android开发。

主要功能有: 读取联系人姓名、号码,并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();  
    }  

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值