android 2.2 擷取連絡人,電話,並撥號

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.