擷取手機通訊錄放入PinnedSectionListView中,按名字首字母排序,並且實現撥打到電話功能。,DingTalk撥打非通訊錄

來源:互聯網
上載者:User

擷取手機通訊錄放入PinnedSectionListView中,按名字首字母排序,並且實現撥打到電話功能。,DingTalk撥打非通訊錄

  1 package com.lixu.tongxunlu;  2   3 import java.util.ArrayList;  4 import com.lixu.tongxunlu.PinnedSectionListView.PinnedSectionListAdapter;  5 import android.app.Activity;  6 import android.content.ContentResolver;  7 import android.content.Context;  8 import android.content.Intent;  9 import android.database.Cursor; 10 import android.graphics.Color; 11 import android.net.Uri; 12 import android.os.Bundle; 13 import android.provider.ContactsContract; 14 import android.view.LayoutInflater; 15 import android.view.View; 16 import android.view.ViewGroup; 17 import android.view.Window; 18 import android.view.WindowManager; 19 import android.widget.AdapterView; 20 import android.widget.AdapterView.OnItemClickListener; 21 import android.widget.ArrayAdapter; 22 import android.widget.TextView; 23  24 public class MainActivity extends Activity { 25     private ArrayList<Contacts> contacts; 26     private ArrayList<Data> data; 27  28     @Override 29     protected void onCreate(Bundle savedInstanceState) { 30         super.onCreate(savedInstanceState); 31         // 去掉標題頭 32         requestWindowFeature(Window.FEATURE_NO_TITLE); 33         setContentView(R.layout.activity_main); 34         // 設定程式全屏 35         toggleFullscreen(true); 36  37         data = new ArrayList<Data>(); 38         contacts = new ArrayList<Contacts>(); 39         // 擷取手機連絡人後裝到contacts中 40         getcontacts(contacts); 41  42         int A = (int) 'A'; 43         for (int i = 0; i < 26; i++) { 44             int letter = A + i; 45             char c = (char) letter; 46  47             Data group = new Data(); 48  49             group.type = Data.GROUP; 50             group.text = c + ""; 51             data.add(group); 52             for (int j = 0; j < contacts.size(); j++) { 53                 Contacts cc = contacts.get(j); 54                 String ss = cc.firstLetterofNmae(); 55                 // 判斷連絡人名字首字母是否和組名字母一樣,一樣後加入本組 56                 if (ss.equals(group.text)) { 57                     Data child = new Data(); 58                     child.type = Data.CHILD; 59                     child.text = cc.name + "" + cc.getPhoneNumbers(); 60                     child.contacts = cc; 61                     data.add(child); 62                 } 63  64             } 65  66         } 67  68         PinnedSectionListView pslv = (PinnedSectionListView) findViewById(R.id.pslv); 69  70         pslv.setAdapter(new MyAdapter(this, -1)); 71  72         pslv.setOnItemClickListener(new OnItemClickListener() { 73  74             @Override 75             public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 76                 // 判斷點擊子功能表才會觸發點擊事件 77                 if (data.get(position).type == Data.CHILD) { 78                     Data datas = data.get(position); 79                     Contacts contact = datas.contacts; 80                     // 擷取電話號碼 81                     String number = contact.getPhoneNumbers(); 82  83                     Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number)); 84                     startActivity(intent); 85                 } 86             } 87         }); 88  89     } 90  91     // 設定程式全屏的方法 92     public void toggleFullscreen(boolean fullScreen) { 93         // fullScreen為true時全屏 94  95         WindowManager.LayoutParams attrs = getWindow().getAttributes(); 96  97         if (fullScreen) { 98             attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 99         } else {100             attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;101         }102 103         getWindow().setAttributes(attrs);104     }105 106     private class Data {107         public static final int GROUP = 0;108         public static final int CHILD = 1;109         public static final int STYE = 2;110         public String text;111         public int type;112         public Contacts contacts = null;113 114     }115 116     private class MyAdapter extends ArrayAdapter implements PinnedSectionListAdapter {117         private LayoutInflater flater;118 119         public MyAdapter(Context context, int resource) {120             super(context, resource);121             flater = LayoutInflater.from(context);122         }123 124         @Override125         public int getCount() {126             return data.size();127         }128 129         @Override130         public Object getItem(int position) {131             return data.get(position);132         }133 134         @Override135         public int getItemViewType(int position) {136             return data.get(position).type;137         }138 139         @Override140         public int getViewTypeCount() {141             return Data.STYE;142         }143 144         @Override145         public View getView(int position, View convertView, ViewGroup parent) {146             int type = getItemViewType(position);147             Data data = (Data) getItem(position);148             switch (type) {149             case Data.GROUP:150                 if (convertView == null)151                     convertView = flater.inflate(android.R.layout.simple_list_item_1, null);152 153                 TextView tv = (TextView) convertView.findViewById(android.R.id.text1);154                 tv.setBackgroundColor(Color.GREEN);155                 tv.setTextSize(35);156                 tv.setText(data.text);157 158                 break;159 160             case Data.CHILD:161                 if (convertView == null)162                     convertView = flater.inflate(android.R.layout.simple_list_item_1, null);163 164                 TextView tv1 = (TextView) convertView.findViewById(android.R.id.text1);165                 tv1.setTextSize(20);166                 tv1.setText(data.text);167 168                 break;169 170             default:171                 break;172             }173             return convertView;174         }175 176         @Override177         public boolean isItemViewTypePinned(int viewType) {178             boolean type = false;179             switch (viewType) {180             case Data.CHILD:181 182                 type = false;183 184                 break;185 186             case Data.GROUP:187 188                 type = true;189 190                 break;191 192             default:193                 break;194             }195             return type;196         }197 198     }199 200     // 擷取手機連絡人的方法201     private void getcontacts(ArrayList<Contacts> contacts) {202         Uri uri = Uri.parse("content://com.android.contacts/contacts");203         ContentResolver resolver = this.getContentResolver();204         // 給query傳遞一個SORT_KEY_PRIMARY,讓ContentResolver將獲得的資料按照連絡人名字首字母排序205         Cursor cursor = resolver.query(uri, null, null, null,206                 android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY);207         while (cursor.moveToNext()) {208             // 連絡人的id209             String id = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));210             // 將連絡人按姓名首字母分組211             String sort_key_primary = cursor212                     .getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY));213             // 擷取連絡人的名字214             String name = cursor215                     .getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME));216             Contacts mContacts = new Contacts();217             mContacts.id = id;218             mContacts.name = name;219             mContacts.sort_key_primary = sort_key_primary;220             // 擷取連絡人手機號碼221             Cursor phone = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,222                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null);223             // 將擷取到的號碼遍曆224             ArrayList<String> phonenumbers = new ArrayList<String>();225             while (phone.moveToNext()) {226                 int phoneFieldColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);227                 String phonenumber = phone.getString(phoneFieldColumnIndex);228                 phonenumbers.add(phonenumber);229             }230             mContacts.phonenumbers = phonenumbers;231             contacts.add(mContacts);232 233         }234     }235 236     private class Contacts {237         public String id;238         public String name;239         public String sort_key_primary;240         public ArrayList<String> phonenumbers;241 242         // 擷取漢字首字母轉換成大寫字母243         public String firstLetterofNmae() {244             String s = sort_key_primary.charAt(0) + "";245             return s.toUpperCase();246 247         }248 249         public String getPhoneNumbers() {250             String phones = "";251             for (int i = 0; i < phonenumbers.size(); i++) {252                 phones += ":" + phonenumbers.get(i);253             }254             return phones;255 256         }257     }258 259 }

xml檔案:

不要忘記添加各種許可權。

 

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:padding="10dp" > 6  7     <com.lixu.tongxunlu.PinnedSectionListView 8         android:id="@+id/pslv" 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content" />11 12 </RelativeLayout>

效果樣本圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.