import android.content.Context;import android.os.AsyncTask;import com.dongnao.a7zipandroid.command.CommandUtils;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;public class ZipHelper {    /**     * 执行结果回调     */    public interface OnResultListener {        void onSuccess(String msg);        void onFailure(int errorno, String msg);        void onProgress(String msg);    }    /**     * 将可执行文件 从assets拷贝到 /data/data/包名 下     *     * @param context     * @param binaryName     * @return     */    public static boolean loadBinary(Context context, String binaryName) {        ///data/data/包名        File binaryFile = new File(context.getFilesDir(), binaryName);        if (binaryFile.exists()) {            //存在 但不能执行            if (!binaryFile.canExecute()) {                //设置可执行并 返回加过                binaryFile.setExecutable(true);            }        } else {            //根据cpu abi拷贝可执行文件            if (CommandUtils.copyAssets2File(context, binaryName)) {                if (!binaryFile.canExecute()) {                    //设置可执行并 返回加过                    binaryFile.setExecutable(true);                }            }        }        return binaryFile.exists() && binaryFile.canExecute();    }    public static void execute(Context context, String cmd, OnResultListener listener) {        File filesDir = context.getFilesDir();        // /data/data/包名/7zr        new ExecuteAysnTask(filesDir.getAbsolutePath() + "/" + cmd, listener).execute();    }    /**     * 结果记录     */    static class Result {        boolean success;        String output;        int errorno;        public Result(boolean success, String output, int errorno) {            this.success = success;            this.output = output;            this.errorno = errorno;        }    }    static class ExecuteAysnTask extends AsyncTask, String, Result> {        private OnResultListener listener;        private String cmd;        public ExecuteAysnTask(String cmd, OnResultListener listener) {            this.cmd = cmd;            this.listener = listener;        }        @Override        protected Result doInBackground(Void... voids) {            Process process = null;            //执行结果输出            String out;            try {                //执行任务                process = Runtime.getRuntime().exec(cmd);                //查看是否执行完成                while (!isComplete(process)) {                    //读取运行过程中的输出信息                    BufferedReader reader = new BufferedReader(new InputStreamReader                            (process.getInputStream()));                    String line;                    while ((line = reader.readLine()) != null) {                        //报告执行过程                        publishProgress(line);                    }                }                int exitValue = process.exitValue();                //成功                if (exitValue == 0) {                    out = CommandUtils.inputStream2String(process.getInputStream());                } else {                    out = CommandUtils.inputStream2String(process.getErrorStream());                }                return new Result(exitValue == 0, out, exitValue);            } catch (IOException e) {                e.printStackTrace();                out = e.getMessage();            } finally {                if (null != process) {                    process.destroy();                }            }            return new Result(false, out, -1);        }        @Override        protected void onProgressUpdate(String... values) {            listener.onProgress(values[0]);        }        @Override        protected void onPostExecute(Result result) {            if (result.success) {                listener.onSuccess(result.output);            } else {                listener.onFailure(result.errorno, result.output);            }        }        /**         * 查看程序是否结束         *         * @param process         * @return         */        private boolean isComplete(Process process) {            try {                //如果已经结束则返回结果 否则会出现IllegalThreadStateException异常                process.exitValue();                return true;            } catch (IllegalThreadStateException e) {            }            return false;        }    }}
   
   
import android.content.Context;import android.os.Build;import android.text.TextUtils;import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class CommandUtils {    /**     * 拷贝文件     *     * @param context     * @param binary     * @return     */    public static boolean copyAssets2File(Context context, String binary) {        // /data/data/package        File filesDirectory = context.getFilesDir();        boolean ret = false;        InputStream is = null;        FileOutputStream fos = null;        try {            //根据cpu 拷贝不同的可执行文件            if (Build.CPU_ABI.startsWith("armeabi")) {                is = context.getAssets().open("libs/armeabi/" + binary);            } else if (Build.CPU_ABI.equals("x86")) {                is = context.getAssets().open("libs/" + Build.CPU_ABI + "/" + binary);            } else {                return false;            }            fos = new FileOutputStream(new File(filesDirectory,                    binary));            byte[] buffer = new byte[2048];            int n;            while ((n = is.read(buffer)) != -1) {                fos.write(buffer, 0, n);            }            fos.flush();            ret = true;        } catch (IOException e) {            e.printStackTrace();        } finally {            if (null != is) {                try {                    is.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (null != fos) {                try {                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return ret;    }    public static String inputStream2String(InputStream inputStream) {        try {            BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));            String str;            StringBuilder sb = new StringBuilder();            while ((str = r.readLine()) != null) {                sb.append(str);            }            return sb.toString();        } catch (IOException e) {            e.printStackTrace();        }        return null;    }}

更多相关文章

  1. 理解Android(安卓)UI线程
  2. 自定义线程池ThreadPoolExecutor
  3. android之popupwindow显示文件列表
  4. Android:StartActivies(Intent[] intents)用法
  5. android 在WebView打开网页
  6. Android(安卓)可执行文件结构的分析
  7. EditText与退格键的冲突问题
  8. Android兼容性优化-8.0之后禁止在后台启动服务的兼容性优化
  9. Monkey入门之如何在android虚拟机中安装apk包

随机推荐

  1. Android(安卓)- SharedPreferences共享数
  2. Android系列之Intent传递对象的几种实例
  3. [Android]在android虚拟机中安装apk手机
  4. Android中实现Runnable接口简单例子
  5. 嵌入式linux和嵌入式android系统有什么区
  6. 如何获取Android系统时间是24小时制还是1
  7. Android(安卓)ListView中动态显示和隐藏H
  8. Android(安卓)Fragment基本用法
  9. Android小程序-模拟小球平抛落地反弹到静
  10. android java数组应用与说明