[Java] [Java]执行命令行语句 →→→→→进入此内容的聊天室

来自 , 2021-03-22, 写在 Java, 查看 133 次.
URL http://www.code666.cn/view/285f89b8
  1. package net.qiujuer.libraries.genius.command;
  2.  
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.IBinder;
  8.  
  9. import net.qiujuer.libraries.genius.journal.LogUtil;
  10. import net.qiujuer.libraries.genius.utils.GlobalValue;
  11.  
  12. import java.util.concurrent.locks.Condition;
  13. import java.util.concurrent.locks.Lock;
  14. import java.util.concurrent.locks.ReentrantLock;
  15.  
  16. /**
  17.  * Created by Genius on 2014/8/13.
  18.  * 命令执行Model
  19.  */
  20. public class CommandModel {
  21.     private static final String TAG = CommandModel.class.getName();
  22.     //调用服务接口
  23.     private static ICommandInterface iService = null;
  24.     //服务链接类,用于实例化服务接口
  25.     private static ServiceConnection conn = new ServiceConnection() {
  26.         @Override
  27.         public void onServiceConnected(ComponentName name, IBinder service) {
  28.             iLock.lock();
  29.             iService = ICommandInterface.Stub.asInterface(service);
  30.             if (iService != null) {
  31.                 try {
  32.                     iCondition.signalAll();
  33.                 } catch (Exception e) {
  34.                     e.printStackTrace();
  35.                 }
  36.             } else
  37.                 bindService();
  38.             iLock.unlock();
  39.             LogUtil.i(TAG, "onServiceConnected");
  40.         }
  41.  
  42.         @Override
  43.         public void onServiceDisconnected(ComponentName name) {
  44.             iService = null;
  45.             LogUtil.i(TAG, "onServiceDisconnected");
  46.         }
  47.     };
  48.     //锁
  49.     private static Lock iLock = new ReentrantLock();
  50.     //等待与唤醒
  51.     private static Condition iCondition = iLock.newCondition();
  52.  
  53.     //执行参数
  54.     private String parameter;
  55.     //是否取消测试
  56.     private boolean isCancel;
  57.  
  58.     /**
  59.      * 实例化
  60.      *
  61.      * @param params @param params 命令参数 eg: "/system/bin/ping", "-c", "4", "-s", "100","www.qiujuer.net"
  62.      */
  63.     public CommandModel(String... params) {
  64.         //check params
  65.         if (params == null)
  66.             throw new NullPointerException();
  67.         //run
  68.         StringBuilder sb = new StringBuilder();
  69.         for (String str : params) {
  70.             sb.append(str);
  71.             sb.append(" ");
  72.         }
  73.         this.parameter = sb.toString();
  74.     }
  75.  
  76.     /**
  77.      * 执行测试
  78.      *
  79.      * @param model ProcessModel
  80.      * @return 结果
  81.      */
  82.     public static String command(CommandModel model) {
  83.         //检测是否取消测试
  84.         if (model.isCancel)
  85.             return null;
  86.         //check Service
  87.         if (iService == null) {
  88.             iLock.lock();
  89.             try {
  90.                 iCondition.await();
  91.             } catch (InterruptedException e) {
  92.                 e.printStackTrace();
  93.             }
  94.             iLock.unlock();
  95.         }
  96.  
  97.         String result;
  98.         try {
  99.             result = iService.command(model.parameter);
  100.         } catch (Exception e) {
  101.             e.printStackTrace();
  102.             bindService();
  103.             result = command(model);
  104.         }
  105.         return result;
  106.     }
  107.  
  108.     /**
  109.      * 启动并绑定服务
  110.      */
  111.     private static void bindService() {
  112.         Context context = GlobalValue.getContext();
  113.         Intent intent = new Intent(context, CommandService.class);
  114.         context.startService(intent);
  115.         context.bindService(intent, conn, Context.BIND_AUTO_CREATE);
  116.     }
  117.  
  118.     /**
  119.      * 取消测试
  120.      */
  121.     public void cancel() {
  122.         isCancel = true;
  123.     }
  124.  
  125.     /**
  126.      * 静态初始化
  127.      */
  128.     static {
  129.         bindService();
  130.     }
  131.  
  132. }//源代码片段来自云代码http://yuncode.net
  133.                        

回复 "[Java]执行命令行语句"

这儿你可以回复上面这条便签

captcha