Skip to content

Commit d29ffe5

Browse files
committed
[commit] add HandlerActivity
1 parent ddf8c24 commit d29ffe5

File tree

5 files changed

+114
-20
lines changed

5 files changed

+114
-20
lines changed

README.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# 常用工具类
2-
- 每一个Android开发者在日常开发中都会积累一些自己的代码片段。
3-
- 本项目就是收集一些日常开发中的常用工具类,减少功能性代码的重复编写,方便快速开发。
4-
- 通过封装一层工具类的好处是以后只需要修改工具类的实现,就可以替换整体的框架。
5-
- 参考了众多项目并加以改进和总结,如有疑问,请来信说明。
2+
- 每一个Android开发者在日常开发中都会积累一些自己的代码片段.
3+
- 本项目就是收集一些日常开发中的常用工具类,减少功能性代码的重复编写,方便快速开发.
4+
- 通过封装一层工具类的好处是以后只需要修改工具类的实现,就可以替换整体的框架.
5+
- 部分方法没有经过测试,请使用者自行测试并反馈问题.
6+
- 参考了众多项目并加以改进和总结,如有疑问,请来信说明.
67

8+
## app Module中的类:
9+
MainActivity.java 测试项目中的主类
10+
HandlerActivity.java Handler的正确使用方式事例
11+
12+
## library Module中的类:
13+
714
- CipherUtils.java 加密工具
815
* md5Encode:md5加密
916
* base64Encode:Base64加密
@@ -79,6 +86,7 @@
7986
* captureView 截图
8087
* createViewBitmap 截图
8188
* convertViewToBitmap 截图
89+
* getActivityBitmap 获取Activity的截图
8290
* getStatusBarHeight 获取状态栏高度
8391
* getToolbarHeight 获取工具栏高度
8492
* getNavigationBarHeight 获取导航栏高度
@@ -151,17 +159,17 @@
151159
License
152160
----
153161

154-
Copyright (C) 2016 android@19code.com
155-
156-
Licensed under the Apache License, Version 2.0 (the "License");
157-
you may not use this file except in compliance with the License.
158-
You may obtain a copy of the License at
159-
160-
http://www.apache.org/licenses/LICENSE-2.0
161-
162-
Unless required by applicable law or agreed to in writing, software
163-
distributed under the License is distributed on an "AS IS" BASIS,
164-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
165-
See the License for the specific language governing permissions and
166-
limitations under the License.
162+
Copyright (C) 2016 android@19code.com
163+
164+
Licensed under the Apache License, Version 2.0 (the "License");
165+
you may not use this file except in compliance with the License.
166+
You may obtain a copy of the License at
167+
168+
http://www.apache.org/licenses/LICENSE-2.0
169+
170+
Unless required by applicable law or agreed to in writing, software
171+
distributed under the License is distributed on an "AS IS" BASIS,
172+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
173+
See the License for the specific language governing permissions and
174+
limitations under the License.
167175

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (C) 2016 android@19code.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.code19.androidcommon;
18+
19+
import android.os.Bundle;
20+
import android.os.Handler;
21+
import android.os.Message;
22+
import android.os.PersistableBundle;
23+
import android.support.v7.app.AppCompatActivity;
24+
25+
import java.lang.ref.WeakReference;
26+
27+
/**
28+
* Create by h4de5ing 2016/5/10 010
29+
* 论Handler的正确使用方式, 参考自:https://yq.aliyun.com/articles/3009 Android 内存泄漏总结
30+
*/
31+
public class HandlerActivity extends AppCompatActivity {
32+
private static class MyHandler extends Handler {
33+
private final WeakReference<HandlerActivity> mMainActivityWeakReference;
34+
35+
public MyHandler(HandlerActivity activity) {
36+
mMainActivityWeakReference = new WeakReference<HandlerActivity>(activity);
37+
}
38+
39+
@Override
40+
public void handleMessage(Message msg) {
41+
super.handleMessage(msg);
42+
HandlerActivity activity = mMainActivityWeakReference.get();
43+
if (activity != null) {
44+
// 处理msg
45+
switch (msg.what) {
46+
case 0:
47+
break;
48+
case 1:
49+
break;
50+
case 2:
51+
break;
52+
}
53+
54+
}
55+
}
56+
}
57+
58+
59+
private final MyHandler mMyHandler = new MyHandler(this);
60+
private static final Runnable sRunnable = new Runnable() {
61+
@Override
62+
public void run() {
63+
// 执行的内容
64+
Message message = new Message();
65+
message.arg1 = 1;
66+
}
67+
};
68+
69+
@Override
70+
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
71+
super.onCreate(savedInstanceState, persistentState);
72+
mMyHandler.postDelayed(sRunnable, 1000 * 6);//6秒钟后执行
73+
finish();
74+
}
75+
}

app/src/main/java/com/code19/androidcommon/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.code19.androidcommon;
22

3-
import android.support.v7.app.AppCompatActivity;
43
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
55

66
public class MainActivity extends AppCompatActivity {
77

library/src/main/java/com/code19/library/SystemUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public static String getSign(Context context, String pkgName) {
193193
/**
194194
* 将签名字符串转换成需要的32位签名
195195
*/
196-
private static String hexdigest(byte[] paramArrayOfByte) {
196+
public static String hexdigest(byte[] paramArrayOfByte) {
197197
final char[] hexDigits = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102};
198198
try {
199199
MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
@@ -209,6 +209,7 @@ private static String hexdigest(byte[] paramArrayOfByte) {
209209
arrayOfChar[++j] = hexDigits[(k & 0xF)];
210210
}
211211
} catch (Exception e) {
212+
e.printStackTrace();
212213
}
213214
return "";
214215
}
@@ -284,7 +285,7 @@ public static int gc(Context cxt) {
284285
* @param title 快捷方式标题
285286
* @param cls 要启动的类
286287
*/
287-
public void createDeskShortCut(Context cxt, int icon, String title, Class<?> cls) {
288+
public static void createDeskShortCut(Context cxt, int icon, String title, Class<?> cls) {
288289
// 创建快捷方式的Intent
289290
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
290291
// 不允许重复创建

library/src/main/java/com/code19/library/ViewUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ public static Bitmap convertViewToBitmap(View view) {
196196
return view.getDrawingCache();
197197
}
198198

199+
/**
200+
* 获取activity的截图
201+
*/
202+
public static Bitmap getActivityBitmap(Activity activity) {
203+
View view = activity.getWindow().getDecorView().findViewById(android.R.id.content);
204+
view.setDrawingCacheEnabled(true);
205+
return view.getDrawingCache();
206+
}
207+
199208
/**
200209
* 获取状态栏的高度
201210
*
@@ -245,4 +254,5 @@ public static Point getScreenSize(Context context) {
245254
wm.getDefaultDisplay().getSize(screenSize);
246255
return screenSize;
247256
}
257+
248258
}

0 commit comments

Comments
 (0)