该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> listItem = new ArrayList>();       
        ArrayList> listItemRead = new ArrayList>();    
        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);     
        while (cursor.moveToNext())   
        {    
         HashMap map = new HashMap();
         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();  
    }  

 

更多相关文章

  1. Android(安卓)Studio使用说明
  2. 在Ubuntu12.10-32bit上构建、测试WebRTC之Android示例WebRTCDemo
  3. Android(安卓)开源项目分类汇总
  4. Widget动态换背景图片 android
  5. Android(安卓)API 28 使用 android-async-http1.4.8 报错
  6. Android(安卓)动态获取创建与删除文件权限
  7. AndroidStudio使用leakcanary教程
  8. Android(安卓)UI开源框架
  9. AndroidStudio安装及配置Gradle遇到的Error及解决办法

随机推荐

  1. qq android 软件 测试结果
  2. google地图示例
  3. VMware 虚拟机图文安装和配置 AlmaLinux
  4. 三元运算符
  5. TP 商城分类页、列表页(七)
  6. api接口和js数据获取及vue商品视图渲染
  7. classList对象与使用blur进行表单非空验
  8. CSS:flex布局:项目上的属性
  9. 自定义方法实现通过类名获取所有元素的对
  10. CSS: flex布局:容器上的属性