提交 08843c3b 编写于 作者: C cmj

see 11/30 log

上级 9a840102
......@@ -192,7 +192,10 @@ getFileCharsetSimple : 简单获取文件编码格式
getFileLines : 获取文件行数
readFile2List : 指定编码按行读取文件到List
readFile2SB : 指定编码按行读取文件到StringBuilder中
getDirSize : 获取目录大小
getFileSize : 获取文件大小
getDirLength : 获取目录长度
getFileLength : 获取文件长度
getFileMD5, getFileMD5ToString : 获取文件的MD5校验码
getDirName : 根据全路径获取最长目录
getFileName : 根据全路径获取文件名
......@@ -373,8 +376,12 @@ getSDCardInfo : 获取SD卡信息
> - **服务相关→[ServiceUtils.java][service.java]**
```
isServiceRunning : 判断服务是否运行
stopService : 停止服务
getAllRunningService : 获取所有运行的服务
startService : 启动服务
stopService : 停止服务
bindService : 绑定服务
unbindService : 解绑服务
isServiceRunning : 判断服务是否运行
```
> - **Shell相关→[ShellUtils.java][shell.java]**
......
......@@ -192,7 +192,10 @@ getFileCharsetSimple
getFileLines
readFile2List
readFile2SB
getDirSize
getFileSize
getDirLength
getFileLength
getFileMD5, getFileMD5ToString
getDirName
getFileName
......@@ -373,8 +376,12 @@ getSDCardInfo
> - **About Service→[ServiceUtils.java][service.java]**
```
isServiceRunning
getAllRunningService
startService
stopService
bindService
unbindService
isServiceRunning
```
> - **About Shell→[ShellUtils.java][shell.java]**
......@@ -641,4 +648,5 @@ limitations under the License.
[zip.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/ZipUtils.java
[zip.test]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/test/java/com/blankj/utilcode/utils/ZipUtilsTest.java
[group]: http://www.jianshu.com/p/8938015df951
[weibo]: http://weibo.com/blankcmj
#### 16/11/30
#### 16/11/23 LocationUtils测试完毕,发布1.3.4
#### 16/11/22 查看LocationActivity内存泄漏
#### 16/11/21 优化README
......
package com.blankj.utilcode.utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
......
......@@ -145,7 +145,7 @@ public class FileUtils {
/**
* 判断目录是否存在,不存在则判断是否创建成功
*
* @param dirPath 文件路径
* @param dirPath 目录路径
* @return {@code true}: 存在或创建成功<br>{@code false}: 不存在或创建失败
*/
public static boolean createOrExistsDir(String dirPath) {
......@@ -505,10 +505,13 @@ public class FileUtils {
* @return 文件链表
*/
public static List<File> listFilesInDir(File dir, boolean isRecursive) {
if (!isDir(dir)) return null;
if (isRecursive) return listFilesInDir(dir);
if (dir == null || !isDir(dir)) return null;
List<File> list = new ArrayList<>();
Collections.addAll(list, dir.listFiles());
File[] files = dir.listFiles();
if (files != null && files.length != 0) {
Collections.addAll(list, files);
}
return list;
}
......@@ -529,7 +532,7 @@ public class FileUtils {
* @return 文件链表
*/
public static List<File> listFilesInDir(File dir) {
if (dir == null || !isDir(dir)) return null;
if (!isDir(dir)) return null;
List<File> list = new ArrayList<>();
File[] files = dir.listFiles();
if (files != null && files.length != 0) {
......@@ -1015,6 +1018,27 @@ public class FileUtils {
return count;
}
/**
* 获取目录大小
*
* @param dirPath 目录路径
* @return 文件大小
*/
public static String getDirSize(String dirPath) {
return getDirSize(getFileByPath(dirPath));
}
/**
* 获取目录大小
*
* @param dir 目录
* @return 文件大小
*/
public static String getDirSize(File dir) {
long len = getDirLength(dir);
return len == -1 ? "" : ConvertUtils.byte2FitSize(len);
}
/**
* 获取文件大小
*
......@@ -1032,8 +1056,61 @@ public class FileUtils {
* @return 文件大小
*/
public static String getFileSize(File file) {
if (!isFileExists(file)) return "";
return ConvertUtils.byte2FitSize(file.length());
long len = getFileLength(file);
return len == -1 ? "" : ConvertUtils.byte2FitSize(len);
}
/**
* 获取目录长度
*
* @param dirPath 目录路径
* @return 文件大小
*/
public static long getDirLength(String dirPath) {
return getDirLength(getFileByPath(dirPath));
}
/**
* 获取目录长度
*
* @param dir 目录
* @return 文件大小
*/
public static long getDirLength(File dir) {
if (!isDir(dir)) return -1;
long len = 0;
File[] files = dir.listFiles();
if (files != null && files.length != 0) {
for (File file : files) {
if (file.isDirectory()) {
len += getDirLength(file);
}else {
len += file.length();
}
}
}
return len;
}
/**
* 获取文件长度
*
* @param filePath 文件路径
* @return 文件大小
*/
public static long getFileLength(String filePath) {
return getFileLength(getFileByPath(filePath));
}
/**
* 获取文件长度
*
* @param file 文件
* @return 文件大小
*/
public static long getFileLength(File file) {
if (!isFile(file)) return -1;
return file.length();
}
/**
......
......@@ -5,6 +5,7 @@ import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
......@@ -55,28 +56,21 @@ public class ServiceUtils {
*/
public static void startService(Context context, String className) {
try {
Intent intent = new Intent(context, Class.forName(className));
context.startService(intent);
startService(context, Class.forName(className));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 判断服务是否运行
* 启动服务
*
* @param context 上下文
* @param className 完整包名的服务类名
* @return {@code true}: 是<br>{@code false}: 否
* @param context 上下文
* @param cls 服务类
*/
public static boolean isServiceRunning(Context context, String className) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> infos = activityManager.getRunningServices(0x7FFFFFFF);
if (infos == null || infos.size() == 0) return false;
for (RunningServiceInfo info : infos) {
if (className.equals(info.service.getClassName())) return true;
}
return false;
public static void startService(Context context, Class<?> cls) {
Intent intent = new Intent(context, cls);
context.startService(intent);
}
/**
......@@ -88,11 +82,94 @@ public class ServiceUtils {
*/
public static boolean stopService(Context context, String className) {
try {
Intent intent = new Intent(context, Class.forName(className));
return context.stopService(intent);
return stopService(context, Class.forName(className));
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 停止服务
*
* @param context 上下文
* @param cls 服务类
* @return {@code true}: 停止成功<br>{@code false}: 停止失败
*/
public static boolean stopService(Context context, Class<?> cls) {
Intent intent = new Intent(context, cls);
return context.stopService(intent);
}
/**
* 绑定服务
*
* @param context 上下文
* @param className 完整包名的服务类名
* @param conn 服务连接对象
* @param flags 绑定选项
* <ul>
* <li>{@link Context#BIND_AUTO_CREATE}</li>
* <li>{@link Context#BIND_DEBUG_UNBIND}</li>
* <li>{@link Context#BIND_NOT_FOREGROUND}</li>
* <li>{@link Context#BIND_ABOVE_CLIENT}</li>
* <li>{@link Context#BIND_ALLOW_OOM_MANAGEMENT}</li>
* <li>{@link Context#BIND_WAIVE_PRIORITY}</li>
* </ul>
*/
public static void bindService(Context context, String className, ServiceConnection conn, int flags) {
try {
bindService(context, Class.forName(className), conn, flags);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 绑定服务
*
* @param context 上下文
* @param cls 服务类
* @param conn 服务连接对象
* @param flags 绑定选项
* <ul>
* <li>{@link Context#BIND_AUTO_CREATE}</li>
* <li>{@link Context#BIND_DEBUG_UNBIND}</li>
* <li>{@link Context#BIND_NOT_FOREGROUND}</li>
* <li>{@link Context#BIND_ABOVE_CLIENT}</li>
* <li>{@link Context#BIND_ALLOW_OOM_MANAGEMENT}</li>
* <li>{@link Context#BIND_WAIVE_PRIORITY}</li>
* </ul>
*/
public static void bindService(Context context, Class<?> cls, ServiceConnection conn, int flags) {
Intent intent = new Intent(context, cls);
context.bindService(intent, conn, flags);
}
/**
* 解绑服务
*
* @param context 上下文
* @param conn 服务连接对象
*/
public static void unbindService(Context context, ServiceConnection conn) {
context.unbindService(conn);
}
/**
* 判断服务是否运行
*
* @param context 上下文
* @param className 完整包名的服务类名
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isServiceRunning(Context context, String className) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> infos = activityManager.getRunningServices(0x7FFFFFFF);
if (infos == null || infos.size() == 0) return false;
for (RunningServiceInfo info : infos) {
if (className.equals(info.service.getClassName())) return true;
}
return false;
}
}
\ No newline at end of file
......@@ -194,9 +194,24 @@ public class FileUtilsTest {
System.out.println(new String(readFile2Bytes(path + "UTF8.txt")));
}
@Test
public void testGetDirSize() throws Exception {
assertThat(getDirSize(path)).isEqualTo("73.000B");
}
@Test
public void testGetDirLength() throws Exception {
assertThat(getDirLength(path)).isEqualTo(73);
}
@Test
public void testGetFileSize() throws Exception {
assertThat(getFileSize(path + "UTF8.txt")).isEqualTo("25B");
assertThat(getFileSize(path + "UTF8.txt")).isEqualTo("25.000B");
}
@Test
public void testGetFileLength() throws Exception {
assertThat(getFileLength(path + "UTF8.txt")).isEqualTo(25);
}
@Test
......
package com.blankj.utilcode.utils;
import android.content.Context;
import android.util.SparseArray;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.notification.Failure;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import static com.google.common.truth.Truth.assertThat;
/**
* <pre>
* author: Blankj
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册