Skip to content

Commit 08843c3

Browse files
committed
see 11/30 log
1 parent 9a84010 commit 08843c3

File tree

8 files changed

+214
-30
lines changed

8 files changed

+214
-30
lines changed

README-CN.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ getFileCharsetSimple : 简单获取文件编码格式
192192
getFileLines : 获取文件行数
193193
readFile2List : 指定编码按行读取文件到List
194194
readFile2SB : 指定编码按行读取文件到StringBuilder中
195+
getDirSize : 获取目录大小
195196
getFileSize : 获取文件大小
197+
getDirLength : 获取目录长度
198+
getFileLength : 获取文件长度
196199
getFileMD5, getFileMD5ToString : 获取文件的MD5校验码
197200
getDirName : 根据全路径获取最长目录
198201
getFileName : 根据全路径获取文件名
@@ -373,8 +376,12 @@ getSDCardInfo : 获取SD卡信息
373376

374377
> - **服务相关→[ServiceUtils.java][service.java]**
375378
```
376-
isServiceRunning : 判断服务是否运行
377-
stopService : 停止服务
379+
getAllRunningService : 获取所有运行的服务
380+
startService : 启动服务
381+
stopService : 停止服务
382+
bindService : 绑定服务
383+
unbindService : 解绑服务
384+
isServiceRunning : 判断服务是否运行
378385
```
379386

380387
> - **Shell相关→[ShellUtils.java][shell.java]**

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ getFileCharsetSimple
192192
getFileLines
193193
readFile2List
194194
readFile2SB
195+
getDirSize
195196
getFileSize
197+
getDirLength
198+
getFileLength
196199
getFileMD5, getFileMD5ToString
197200
getDirName
198201
getFileName
@@ -373,8 +376,12 @@ getSDCardInfo
373376

374377
> - **About Service→[ServiceUtils.java][service.java]**
375378
```
376-
isServiceRunning
379+
getAllRunningService
380+
startService
377381
stopService
382+
bindService
383+
unbindService
384+
isServiceRunning
378385
```
379386

380387
> - **About Shell→[ShellUtils.java][shell.java]**
@@ -641,4 +648,5 @@ limitations under the License.
641648
[zip.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/ZipUtils.java
642649
[zip.test]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/test/java/com/blankj/utilcode/utils/ZipUtilsTest.java
643650

651+
[group]: http://www.jianshu.com/p/8938015df951
644652
[weibo]: http://weibo.com/blankcmj

update_log.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#### 16/11/30
12
#### 16/11/23 LocationUtils测试完毕,发布1.3.4
23
#### 16/11/22 查看LocationActivity内存泄漏
34
#### 16/11/21 优化README

utilcode/src/main/java/com/blankj/utilcode/utils/ActivityUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.blankj.utilcode.utils;
22

3+
import android.app.Activity;
34
import android.content.Context;
45
import android.content.Intent;
56
import android.content.pm.PackageManager;

utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public static boolean isFile(File file) {
145145
/**
146146
* 判断目录是否存在,不存在则判断是否创建成功
147147
*
148-
* @param dirPath 文件路径
148+
* @param dirPath 目录路径
149149
* @return {@code true}: 存在或创建成功<br>{@code false}: 不存在或创建失败
150150
*/
151151
public static boolean createOrExistsDir(String dirPath) {
@@ -505,10 +505,13 @@ public static List<File> listFilesInDir(String dirPath, boolean isRecursive) {
505505
* @return 文件链表
506506
*/
507507
public static List<File> listFilesInDir(File dir, boolean isRecursive) {
508+
if (!isDir(dir)) return null;
508509
if (isRecursive) return listFilesInDir(dir);
509-
if (dir == null || !isDir(dir)) return null;
510510
List<File> list = new ArrayList<>();
511-
Collections.addAll(list, dir.listFiles());
511+
File[] files = dir.listFiles();
512+
if (files != null && files.length != 0) {
513+
Collections.addAll(list, files);
514+
}
512515
return list;
513516
}
514517

@@ -529,7 +532,7 @@ public static List<File> listFilesInDir(String dirPath) {
529532
* @return 文件链表
530533
*/
531534
public static List<File> listFilesInDir(File dir) {
532-
if (dir == null || !isDir(dir)) return null;
535+
if (!isDir(dir)) return null;
533536
List<File> list = new ArrayList<>();
534537
File[] files = dir.listFiles();
535538
if (files != null && files.length != 0) {
@@ -1015,6 +1018,27 @@ public static int getFileLines(File file) {
10151018
return count;
10161019
}
10171020

1021+
/**
1022+
* 获取目录大小
1023+
*
1024+
* @param dirPath 目录路径
1025+
* @return 文件大小
1026+
*/
1027+
public static String getDirSize(String dirPath) {
1028+
return getDirSize(getFileByPath(dirPath));
1029+
}
1030+
1031+
/**
1032+
* 获取目录大小
1033+
*
1034+
* @param dir 目录
1035+
* @return 文件大小
1036+
*/
1037+
public static String getDirSize(File dir) {
1038+
long len = getDirLength(dir);
1039+
return len == -1 ? "" : ConvertUtils.byte2FitSize(len);
1040+
}
1041+
10181042
/**
10191043
* 获取文件大小
10201044
*
@@ -1032,8 +1056,61 @@ public static String getFileSize(String filePath) {
10321056
* @return 文件大小
10331057
*/
10341058
public static String getFileSize(File file) {
1035-
if (!isFileExists(file)) return "";
1036-
return ConvertUtils.byte2FitSize(file.length());
1059+
long len = getFileLength(file);
1060+
return len == -1 ? "" : ConvertUtils.byte2FitSize(len);
1061+
}
1062+
1063+
/**
1064+
* 获取目录长度
1065+
*
1066+
* @param dirPath 目录路径
1067+
* @return 文件大小
1068+
*/
1069+
public static long getDirLength(String dirPath) {
1070+
return getDirLength(getFileByPath(dirPath));
1071+
}
1072+
1073+
/**
1074+
* 获取目录长度
1075+
*
1076+
* @param dir 目录
1077+
* @return 文件大小
1078+
*/
1079+
public static long getDirLength(File dir) {
1080+
if (!isDir(dir)) return -1;
1081+
long len = 0;
1082+
File[] files = dir.listFiles();
1083+
if (files != null && files.length != 0) {
1084+
for (File file : files) {
1085+
if (file.isDirectory()) {
1086+
len += getDirLength(file);
1087+
}else {
1088+
len += file.length();
1089+
}
1090+
}
1091+
}
1092+
return len;
1093+
}
1094+
1095+
/**
1096+
* 获取文件长度
1097+
*
1098+
* @param filePath 文件路径
1099+
* @return 文件大小
1100+
*/
1101+
public static long getFileLength(String filePath) {
1102+
return getFileLength(getFileByPath(filePath));
1103+
}
1104+
1105+
/**
1106+
* 获取文件长度
1107+
*
1108+
* @param file 文件
1109+
* @return 文件大小
1110+
*/
1111+
public static long getFileLength(File file) {
1112+
if (!isFile(file)) return -1;
1113+
return file.length();
10371114
}
10381115

10391116
/**

utilcode/src/main/java/com/blankj/utilcode/utils/ServiceUtils.java

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.ComponentName;
66
import android.content.Context;
77
import android.content.Intent;
8+
import android.content.ServiceConnection;
89
import android.content.pm.ResolveInfo;
910
import android.os.Bundle;
1011

@@ -55,28 +56,21 @@ public static Set getAllRunningService(Context context) {
5556
*/
5657
public static void startService(Context context, String className) {
5758
try {
58-
Intent intent = new Intent(context, Class.forName(className));
59-
context.startService(intent);
59+
startService(context, Class.forName(className));
6060
} catch (Exception e) {
6161
e.printStackTrace();
6262
}
6363
}
6464

6565
/**
66-
* 判断服务是否运行
66+
* 启动服务
6767
*
68-
* @param context 上下文
69-
* @param className 完整包名的服务类名
70-
* @return {@code true}: 是<br>{@code false}: 否
68+
* @param context 上下文
69+
* @param cls 服务类
7170
*/
72-
public static boolean isServiceRunning(Context context, String className) {
73-
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
74-
List<RunningServiceInfo> infos = activityManager.getRunningServices(0x7FFFFFFF);
75-
if (infos == null || infos.size() == 0) return false;
76-
for (RunningServiceInfo info : infos) {
77-
if (className.equals(info.service.getClassName())) return true;
78-
}
79-
return false;
71+
public static void startService(Context context, Class<?> cls) {
72+
Intent intent = new Intent(context, cls);
73+
context.startService(intent);
8074
}
8175

8276
/**
@@ -88,11 +82,94 @@ public static boolean isServiceRunning(Context context, String className) {
8882
*/
8983
public static boolean stopService(Context context, String className) {
9084
try {
91-
Intent intent = new Intent(context, Class.forName(className));
92-
return context.stopService(intent);
85+
return stopService(context, Class.forName(className));
9386
} catch (Exception e) {
9487
e.printStackTrace();
9588
return false;
9689
}
9790
}
91+
92+
/**
93+
* 停止服务
94+
*
95+
* @param context 上下文
96+
* @param cls 服务类
97+
* @return {@code true}: 停止成功<br>{@code false}: 停止失败
98+
*/
99+
public static boolean stopService(Context context, Class<?> cls) {
100+
Intent intent = new Intent(context, cls);
101+
return context.stopService(intent);
102+
}
103+
104+
/**
105+
* 绑定服务
106+
*
107+
* @param context 上下文
108+
* @param className 完整包名的服务类名
109+
* @param conn 服务连接对象
110+
* @param flags 绑定选项
111+
* <ul>
112+
* <li>{@link Context#BIND_AUTO_CREATE}</li>
113+
* <li>{@link Context#BIND_DEBUG_UNBIND}</li>
114+
* <li>{@link Context#BIND_NOT_FOREGROUND}</li>
115+
* <li>{@link Context#BIND_ABOVE_CLIENT}</li>
116+
* <li>{@link Context#BIND_ALLOW_OOM_MANAGEMENT}</li>
117+
* <li>{@link Context#BIND_WAIVE_PRIORITY}</li>
118+
* </ul>
119+
*/
120+
public static void bindService(Context context, String className, ServiceConnection conn, int flags) {
121+
try {
122+
bindService(context, Class.forName(className), conn, flags);
123+
} catch (Exception e) {
124+
e.printStackTrace();
125+
}
126+
}
127+
128+
/**
129+
* 绑定服务
130+
*
131+
* @param context 上下文
132+
* @param cls 服务类
133+
* @param conn 服务连接对象
134+
* @param flags 绑定选项
135+
* <ul>
136+
* <li>{@link Context#BIND_AUTO_CREATE}</li>
137+
* <li>{@link Context#BIND_DEBUG_UNBIND}</li>
138+
* <li>{@link Context#BIND_NOT_FOREGROUND}</li>
139+
* <li>{@link Context#BIND_ABOVE_CLIENT}</li>
140+
* <li>{@link Context#BIND_ALLOW_OOM_MANAGEMENT}</li>
141+
* <li>{@link Context#BIND_WAIVE_PRIORITY}</li>
142+
* </ul>
143+
*/
144+
public static void bindService(Context context, Class<?> cls, ServiceConnection conn, int flags) {
145+
Intent intent = new Intent(context, cls);
146+
context.bindService(intent, conn, flags);
147+
}
148+
149+
/**
150+
* 解绑服务
151+
*
152+
* @param context 上下文
153+
* @param conn 服务连接对象
154+
*/
155+
public static void unbindService(Context context, ServiceConnection conn) {
156+
context.unbindService(conn);
157+
}
158+
159+
/**
160+
* 判断服务是否运行
161+
*
162+
* @param context 上下文
163+
* @param className 完整包名的服务类名
164+
* @return {@code true}: 是<br>{@code false}: 否
165+
*/
166+
public static boolean isServiceRunning(Context context, String className) {
167+
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
168+
List<RunningServiceInfo> infos = activityManager.getRunningServices(0x7FFFFFFF);
169+
if (infos == null || infos.size() == 0) return false;
170+
for (RunningServiceInfo info : infos) {
171+
if (className.equals(info.service.getClassName())) return true;
172+
}
173+
return false;
174+
}
98175
}

utilcode/src/test/java/com/blankj/utilcode/utils/FileUtilsTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,24 @@ public void testReadFile2Bytes() throws Exception {
194194
System.out.println(new String(readFile2Bytes(path + "UTF8.txt")));
195195
}
196196

197+
@Test
198+
public void testGetDirSize() throws Exception {
199+
assertThat(getDirSize(path)).isEqualTo("73.000B");
200+
}
201+
202+
@Test
203+
public void testGetDirLength() throws Exception {
204+
assertThat(getDirLength(path)).isEqualTo(73);
205+
}
206+
197207
@Test
198208
public void testGetFileSize() throws Exception {
199-
assertThat(getFileSize(path + "UTF8.txt")).isEqualTo("25B");
209+
assertThat(getFileSize(path + "UTF8.txt")).isEqualTo("25.000B");
210+
}
211+
212+
@Test
213+
public void testGetFileLength() throws Exception {
214+
assertThat(getFileLength(path + "UTF8.txt")).isEqualTo(25);
200215
}
201216

202217
@Test

utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package com.blankj.utilcode.utils;
22

33
import android.content.Context;
4-
import android.util.SparseArray;
54

65
import org.junit.Test;
76
import org.junit.runner.RunWith;
8-
import org.junit.runner.notification.Failure;
97
import org.robolectric.RobolectricTestRunner;
108
import org.robolectric.RuntimeEnvironment;
119
import org.robolectric.annotation.Config;
1210

1311
import java.io.File;
14-
import java.util.HashMap;
15-
import java.util.LinkedHashMap;
1612
import java.util.List;
1713

14+
import static com.google.common.truth.Truth.assertThat;
15+
1816
/**
1917
* <pre>
2018
* author: Blankj

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy