小菜毛毛技術分享

          與大家共同成長

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks

          原創  android 2.2 獲取聯系人,電話,并撥號 收藏

          該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();   
              }   

          posted on 2010-12-17 16:58 小菜毛毛 閱讀(1755) 評論(0)  編輯  收藏 所屬分類: andriod
          主站蜘蛛池模板: 上蔡县| 东丰县| 彭阳县| 宜兰县| 永城市| 永嘉县| 峨山| 安阳市| 双辽市| 太和县| 大关县| 佛山市| 云安县| 曲阳县| 龙海市| 南澳县| 耒阳市| 东兰县| 岑溪市| 香格里拉县| 友谊县| 扬中市| 栾城县| 家居| 犍为县| 铅山县| 唐海县| 千阳县| 吉水县| 孟村| 绵阳市| 盖州市| 惠来县| 定襄县| 民权县| 珠海市| 新疆| 清徐县| 潢川县| 广元市| 贵州省|