小菜毛毛技術分享

          與大家共同成長

            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 小菜毛毛 閱讀(1766) 評論(0)  編輯  收藏 所屬分類: andriod
          主站蜘蛛池模板: 泸定县| 同心县| 定南县| 阿城市| 大连市| 渭源县| 石棉县| 吉木萨尔县| 新余市| 淮阳县| 高碑店市| 靖西县| 高平市| 四子王旗| 江西省| 阿城市| 常熟市| 遵化市| 陈巴尔虎旗| 阜阳市| 汤原县| 邳州市| 东乌| 南宫市| 赫章县| 湘乡市| 崇明县| 景宁| 武平县| 日喀则市| 曲麻莱县| 云安县| 斗六市| 南乐县| 阳高县| 蚌埠市| 灵川县| 中西区| 轮台县| 高邑县| 怀柔区|