摘要: 最近总是有人在问我,Android怎么切图啊,怎么适配啊,不只是Android同行,还有很多新手ui设计师。于是我就写篇文章,根据我们平时的开发经验,简单的介绍一下吧。如果UI设计师以1920*1080为基准来作图,那么直接切出来的图,就可以放到xxh文件夹了。如果UI设计师以1280*70为基准来... 阅读全文
posted @ 2015-11-04 17:36 Andye 阅读(5550) 评论(0) 推荐(2) 编辑

好久没更新文章了,近期在做通讯录上传,把它分享出来,送给需要的朋友。

写了一个通讯录工具类,直接放代码吧,关键位置通过注释来解释。

 

这个工具类包含通讯录获取,加密,然后上传操作。看不懂的可以留言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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();//上报通讯录信息即可
上面解密后的数据日志为(声明:通讯录数据都是假数据,如有雷同,纯属巧合,可联系我删除):

原数据是上面这样,中间是有点的,下面这个被编辑器去掉了


1
2
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

 获取通讯录需要权限,记着提前申请:

1
Manifest.permission.READ_CONTACTS

  

 

----------附上base64位加密后面附带的参数解释----

CRLF 这个参数看起来比较眼熟,它就是Win风格的换行符,意思就是使用CR LF这一对作为一行的结尾而不是Unix风格的LF

DEFAULT 这个参数是默认,使用默认的方法来加密

NO_PADDING 这个参数是略去加密字符串最后的”=”

NO_WRAP 这个参数意思是略去所有的换行符(设置后CRLF就没用了)

URL_SAFE 这个参数意思是加密时不使用对URL和文件名有特殊意义的字符来作为加密字符,具体就是以-和_取代+和/

 

 

----------------------------------------------

 

posted @ 2018-02-27 18:35 Andye 阅读(3554) 评论(1) 推荐(1) 编辑
摘要: Activity的启动模式 阅读全文
posted @ 2017-06-22 16:19 Andye 阅读(452) 评论(0) 推荐(0) 编辑
摘要: 今天有位同事请求帮忙调试微信登录问题,他遇到了以下2个问题,所以,写篇日志备忘,如果有其它朋友遇到此类问题,都可以照此解决! 平时在开发中,有些开发者经常会遇到微信登录SDK登录时,无法调起微信客户端,以及登录完毕后无法回调的情况 这些情况,大概原因如下,请对号入座: 1.包名和签名,跟微信后台登记 阅读全文
posted @ 2017-05-24 18:28 Andye 阅读(6143) 评论(1) 推荐(0) 编辑
摘要: Android报“android.content.res.Resources$NotFoundException: String resource ID #0x2”错误 当调用setText()方法时如果传入int型是不会被当成内容而是resourceID来使用! 所以报错! 解决方法:TextVi 阅读全文
posted @ 2017-05-05 19:04 Andye 阅读(17362) 评论(0) 推荐(5) 编辑
摘要: version 52.0 是java8的环境。当gradle tools 升级到2.2.1时候,可能编译时候会报该错误。 很多网友说更改java version,但是很多时候无效。下面是我遇到时候的解决办法: 解决思路如下: 1.Android studio 中: 出现该问题的情况,大多数是在升级完 阅读全文
posted @ 2016-10-15 21:00 Andye 阅读(7397) 评论(0) 推荐(1) 编辑
摘要: 有时候在提交的时候,中间提交出错,导致有文件被lock,所以会报下面的错误: fatal: Unable to create ‘/msg/.git/index.lock’: File exists. If no other git process is currently running, this 阅读全文
posted @ 2016-03-31 18:20 Andye 阅读(7641) 评论(1) 推荐(3) 编辑
摘要: 今天读到一篇总结的非常棒的文章,写的逻辑很清晰也很实用,很少见到如此棒的文章了。就原文转发过来,我把格式给整理了一下,分享给园子里的各位朋友!好久没写博客了,就为2015年的11月留份纪念吧。希望对你有帮助! 感谢原文作者的无私分享,原文地址:http://www.jcodecraeer.com/a... 阅读全文
posted @ 2015-11-13 15:30 Andye 阅读(10022) 评论(0) 推荐(4) 编辑
摘要: 屏幕适配的注意事项1.AndroidManifest.xml设置在中Menifest中添加子元素android:anyDensity="true"时,应用程序安装在不同密度的终端上时,程序会分别加载xxhdpi、xhdpi、hdpi、mdpi、ldpi文件夹中的资源。相反,如果设为false,即使在... 阅读全文
posted @ 2015-11-04 17:42 Andye 阅读(1223) 评论(0) 推荐(1) 编辑
摘要: 在我们开发过程中,有可能会遇到webview有些网页打不开的问题。这可能是设置的不对,下面就是解决办法。进行如下设置吧,大多数情况都能解决!displayWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//设置... 阅读全文
posted @ 2015-11-04 17:16 Andye 阅读(29502) 评论(1) 推荐(3) 编辑
点击右上角即可分享
微信分享提示