免責事項:この記事はブロガーオリジナル記事ですが、許可ブロガーなく再生してはなりません。https://blog.csdn.net/Coder_Hcy/article/details/85284910
1.アプリケーション情報を取得し、アプリケーションパッケージ名を閉じるためによれば(android7.1.2プロ試験入手可能)
package com.vtronedu.vbox.library.utils;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Debug;
import android.util.Log;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* @创建人:hcy
* @创建时间:2018/12/21
* @作用描述:Function
**/
public class BoxAppUtils {
private static final String TAG = "==BoxApp==";
public static void killApk(Context context) {
forceKillBoxAPPByPackName(ConstantsUtils.ECLASSPACKNAME, context);
}
/**
* 根据包名强制杀死应用
*
* @param appPackName
*/
private static void forceKillBoxAPPByPackName(String appPackName, Context context) {
if (isBoxAppRunning(context, appPackName)) {
try {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
Method method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
method.invoke(am, appPackName);
Log.d(TAG, "强杀进程: " + appPackName + "成功");
} catch (Exception e) {
Log.d(TAG, "强杀进程: " + appPackName + "失败,原因:" + e.getMessage());
e.printStackTrace();
}
} else {
Log.d(TAG, "后台没有" + appPackName + "进程,不需要杀死");
}
}
/**
* 获取进程的信息
*
* @param context
* @return
*/
protected static boolean isBoxAppRunning(Context context, String appPackName) {
List<String> packNameList = new ArrayList<>();
ActivityManager systemService = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = systemService.getRunningAppProcesses();
PackageManager PM = context.getPackageManager();
for (ActivityManager.RunningAppProcessInfo runningAppProcess : runningAppProcesses) {
Log.d(TAG, "getProcessInfo: processName:" + runningAppProcess.processName);
//获取应用的名称
packNameList.add(runningAppProcess.processName);
try {
ApplicationInfo applicationInfo = PM.getApplicationInfo(runningAppProcess.processName, 0);
String appname = applicationInfo.loadLabel(PM).toString();
Drawable appDrawable = applicationInfo.loadIcon(PM);
if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
Log.d(TAG, "getProcessInfo: " + appname + "系统应用");
} else {
Log.d(TAG, "getProcessInfo: " + appname + "不是系统应用");
}
} catch (PackageManager.NameNotFoundException e) {
Log.d(TAG, "getProcessInfo: " + runningAppProcess.processName + "出错");
e.printStackTrace();
}
}
if (packNameList.contains(appPackName)) {
return true;
}
return false;
}
}
2:ファイル/フォルダのサイズを得ます。ファイル拡張子を削除します
package com.ygjy.hcy.boxlib.utils;
import java.io.File;
/**
* @创建人:hcy
* @创建时间:2018/12/20
* @作用描述:Function
* 1:计算某个文件或者文件夹的大小
* 2:去除文件后缀名
**/
public class FileUtils {
/**
* 计算某个文件或者文件夹的大小
* @param file
* @return
*/
public static long getTotalSizeOfFilesInDirFile(final File file) {
if (file.isFile())
return file.length();
final File[] children = file.listFiles();
long total = 0;
if (children != null)
for (final File child : children)
total += getTotalSizeOfFilesInDirFile(child);
return total;
}
/**
* 去除文件后缀名
* @param filename
* @return
*/
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
}
3:進行したファイル/フォルダを削除
package com.ygjy.hcy.toolib;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* @创建人:hcy
* @创建时间:2018/9/26
* @作用描述:Function 文件工具类:文件删除带进度。
**/
public class FileUtil {
private static final String TAG = "=FileUtils=";
private static boolean DeleteFileDir_flag=true;
private static long DeleteFileDir_Total_Size=0;
private static long DeleteFileDir_HasDeleteSize=0;
/**
* 对文件夹进行删除,记录删除的进度
* @param dirPath 文件夹路径
* @param callBack 删除进度的回调
*/
public static void deleteFileDir(String dirPath,FileDeleteCallBack callBack) {
File file = new File(dirPath);
if (!file.exists()) {
Log.d(TAG, "deleteFileDir: 当前要删除文件不存在:" + dirPath);
return;
}
//获得文件大小
if (DeleteFileDir_flag){
DeleteFileDir_Total_Size=0;
DeleteFileDir_Total_Size= getDirSize(file);//没递归文件夹之前读取文件夹的总大小
DeleteFileDir_HasDeleteSize=0;
DeleteFileDir_flag=false;
}
Log.d(TAG, "deleteFileDir: 文件总大小::"+DeleteFileDir_Total_Size);
if (!file.isDirectory()) {
deleteFile(file,callBack);
return;
}
//遍历文件夹
String[] tempList = file.list();
File temp = null;
if (tempList != null && tempList.length != 0) {
for (int i = 0; i < tempList.length; i++) {
if (dirPath.endsWith(File.separator)) {
temp = new File(dirPath + tempList[i]);
} else {
temp = new File(dirPath + File.separator + tempList[i]);
}
if (temp.isFile()) {
deleteFile(temp,callBack);
} else if (temp.isDirectory()) {
deleteFileDir(dirPath + "/" + tempList[i],callBack);//递归删除
}
}
//删除空文件夹
deleteFile(file,callBack);
} else {
//删除空文件夹
deleteFile(file,callBack);
}
//文件删除成功
Log.d(TAG, "deleteFileDir: 文件删除成功:"+dirPath);
if (callBack!=null){
callBack.deleteFileDirSuccess();
}
}
private static void deleteFile(File file,FileDeleteCallBack callBack) {
Log.d(TAG, "deleteFile: 删除文件::" + file.getName() + "??" + file.getAbsolutePath());
DeleteFileDir_HasDeleteSize += file.length();
file.delete();
if (callBack != null) {
callBack.deleteProgress(DeleteFileDir_Total_Size, DeleteFileDir_HasDeleteSize);
}
}
/**
* 获得一个文件夹的大小
* @param file
* @return byte
*/
public static long getDirSize(File file) {
//判断文件是否存在
if (file.exists()) {
//如果是目录则递归计算其内容的总大小
if (file.isDirectory()) {
File[] children = file.listFiles();
long size = 0;
for (File f : children)
size += getDirSize(f);
return size;
} else {//如果是文件则直接返回其大小,以“兆”为单位
long size = file.length();
return size;
}
} else {
return 0;
}
}
/**
* 文件夹删除的回调
*/
public interface FileDeleteCallBack{
/**
*
* @param total_size 文件夹总大小
* @param hasdelete_size 已经删除的大小
*/
void deleteProgress(long total_size,long hasdelete_size);
/**
* 删除文件夹成功
*/
void deleteFileDirSuccess();
}
public static void unzipFile(String zipFilePath,String unzipFilePath,boolean isDeleteZip,UnZipCallBack callBack){
if (TextUtils.isEmpty(zipFilePath) || TextUtils.isEmpty(unzipFilePath))
{
if (callBack!=null){
callBack.unziperror(0);
}
Log.d(TAG, "unzipFile: 压缩路径或者解压缩的路径不规范");
return;
}
File zipFile = new File(zipFilePath);
if (zipFile!=null&&zipFile.exists()){
}else {
if (callBack!=null){
callBack.unziperror(1);
}
Log.d(TAG, "unzipFile: 压缩文件不存在"+zipFilePath);
return;
}
//创建解压缩文件保存的路径
File unzipFileDir = new File(unzipFilePath);
if (!unzipFileDir.exists() || !unzipFileDir.isDirectory())
{
unzipFileDir.mkdirs();
}
//开始解压
ZipEntry entry = null;
String entryFilePath = null, entryDirPath = null;
File entryFile = null, entryDir = null;
int index = 0, count = 0, bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
ZipFile zip = new ZipFile(zipFile);
Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zip.entries();
long totalSize=0;
while (entries.hasMoreElements()){
ZipEntry ziptry = entries.nextElement();
ziptry.getSize();
totalSize+= ziptry.getCompressedSize();
Log.d(TAG, "unzipFile: 获取要解压文件的信息:"+ziptry.getName()+"??"+ziptry.getCompressedSize()+"???"+ziptry.getSize());
}
Log.d(TAG, "unzipFile:解压文件总大小"+totalSize);
//循环对压缩包里的每一个文件进行解压
long prepareSize=0;
while(entries.hasMoreElements()) {
entry = entries.nextElement();
//构建压缩包中一个文件解压后保存的文件全路径
entryFilePath = unzipFilePath + File.separator + entry.getName();
//构建解压后保存的文件夹路径
index = entryFilePath.lastIndexOf(File.separator);
if (index != -1) {
entryDirPath = entryFilePath.substring(0, index);
} else {
entryDirPath = "";
}
entryDir = new File(entryDirPath);
//如果文件夹路径不存在,则创建文件夹
if (!entryDir.exists() || !entryDir.isDirectory()) {
entryDir.mkdirs();
}
//创建解压文件
entryFile = new File(entryFilePath);
if (entryFile.exists()) {
//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException
SecurityManager securityManager = new SecurityManager();
securityManager.checkDelete(entryFilePath);
//删除已存在的目标文件
entryFile.delete();
}
//写入文件
bos = new BufferedOutputStream(new FileOutputStream(entryFile));
bis = new BufferedInputStream(zip.getInputStream(entry));
while ((count = bis.read(buffer, 0, bufferSize)) != -1)
{
if (count!=-1){
prepareSize+=count;
if (callBack!=null){
callBack.unzipProgress(totalSize,prepareSize);
}
}
bos.write(buffer, 0, count);
}
bos.flush();
bos.close();
}
} catch (IOException e) {
Log.d(TAG, "unzipFile: 解压过程中出现异常:"+e.getMessage());
if (callBack!=null){
callBack.unziperror(2);
}
e.printStackTrace();
}
}
/**
* 解压的进度回调
*/
public interface UnZipCallBack{
void unzipProgress(long totalsize,long preparesize);
/**
*
* @param type 0:压缩路径或者解压缩的路径不规范
* 1:压缩文件不存在
* 2:解压过程中出现异常
*/
void unziperror(int type);
}
}
4.有線MACアドレスを取得します。
package com.ygjy.hcy.toolib;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* @创建人:hcy
* @创建时间:2018/9/28
* @作用描述:Function
**/
public class MacAddressUtil {
/**
* 获得有线的mac地址
* @return
*/
public static String getWireMacAddr() {
final String ETH0_MAC_ADDR = "/sys/class/net/eth0/address";
try {
return readLine(ETH0_MAC_ADDR);
} catch (IOException e) {
e.printStackTrace();
return "unavailable";
}
}
private static String readLine(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename), 256);
try {
return reader.readLine();
} finally {
reader.close();
}
}
}
5.オープンWPS(バージョン7.0以下とandroid7.0対応)
package com.ishuidi.boxproject.module.myFile;
public class WpsModel {
public static final String OPEN_MODE = "OpenMode";// 打开文件的模式。
public static final String SEND_SAVE_BROAD = "SendSaveBroad";// 文件保存时是否发送广播。
public static final String SEND_CLOSE_BROAD = "SendCloseBroad";// 文件关闭时是否发送广播
public static final String THIRD_PACKAGE = "ThirdPackage";// 第三方的包名,关闭的广播会包含该项。
public static final String CLEAR_BUFFER = "ClearBuffer";// 关闭文件时是否请空临时文件。
public static final String CLEAR_TRACE = "ClearTrace";// 关闭文件时是否删除使用记录。
public static final String CLEAR_FILE = "ClearFile";// 关闭文件时是否删除打开的文件。
public static final String VIEW_PROGRESS = "ViewProgress";// 文件上次查看的进度。
public static final String AUTO_JUMP = "AutoJump";// 是否自动跳转到上次查看的进度。
public static final String SAVE_PATH = "SavePath";// 文件保存路径。
public static final String VIEW_SCALE = "ViewScale";// 文件上次查看的视图的缩放。
public static final String VIEW_SCALE_X = "ViewScrollX";// 文件上次查看的视图的X坐标。
public static final String VIEW_SCALE_Y = "ViewScrollY";// 文件上次查看的视图的Y坐标。
public static final String USER_NAME = "UserName";// 批注的作者。
public static final String HOMEKEY_DOWN = "HomeKeyDown";// 监听home键并发广播
public static final String BACKKEY_DOWN = "BackKeyDown";// 监听back键并发广播
public static final String ENTER_REVISE_MODE = "EnterReviseMode";// 以修订模式打开文档
public static final String CACHE_FILE_INVISIBLE = "CacheFileInvisible";// Wps生成的缓存文件外部是否可见
public class OpenMode {
public static final String NORMAL = "Normal";// 只读模式
public static final String READ_ONLY = "ReadOnly";// 正常模式
public static final String READ_MODE = "ReadMode";// 打开直接进入阅读器模式
// 仅Word、TXT文档支持
public static final String SAVE_ONLY = "SaveOnly";// 保存模式(打开文件,另存,关闭)
// 仅Word、TXT文档支持
}
public class ClassName {
public static final String NORMAL = "cn.wps.moffice.documentmanager.PreStartActivity2";
// 普通版
public static final String ENGLISH = "cn.wps.moffice.documentmanager.PreStartActivity2";
// 英文版
public static final String ENTERPRISE = "cn.wps.moffice.documentmanager.PreStartActivity2";
// 企业版
}
public class PackageName {
public static final String NORMAL = "cn.wps.moffice_eng";// 普通版
public static final String ENGLISH = "cn.wps.moffice_eng";// 英文版
}
public class Reciver {
public static final String ACTION_BACK = "com.kingsoft.writer.back.key.down";// 返回键广播
public static final String ACTION_HOME = "com.kingsoft.writer.home.key.down";// Home键广播
public static final String ACTION_SAVE = "cn.wps.moffice.file.save";// 保存广播
public static final String ACTION_CLOSE = "cn.wps.moffice.file.close";// 关闭文件广播
}
}
private static void openFileWithWps(File file, Activity activity) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString(WpsModel.OPEN_MODE, WpsModel.OpenMode.READ_MODE);
bundle.putBoolean(WpsModel.SEND_CLOSE_BROAD, true);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName("cn.wps.moffice_eng", "cn.wps.moffice.documentmanager.PreStartActivity2");
//判断是否是AndroidN以及更高的版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri uri = FileProvider.getUriForFile(BoxApplication.getContext(), "com.ishuidi.boxproject.fileprovider", file);
//赋予临时权限
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//设置dataAndType
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.putExtras(bundle);
} else {
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtras(bundle);
}
try {
activity.startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
IntentFilter filter = new IntentFilter();
filter.addAction(WpsModel.Reciver.ACTION_CLOSE);
activity.registerReceiver(new WpsReceiver(activity), filter);
}
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--<external-path path="Android/data/com.ishuidi.boxproject/" name="files_root" /> 适配7以上数据共享《第三方应用下载,打开U盘文件的ppt等格式的文件》-->
<external-path name="external_files" path="."/>
<root-path name="root_files" path=""/>
</paths>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.ishuidi.boxproject.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>