qqhai1986 2015-01-23 06:49 采纳率: 100%
浏览 5493
已采纳

android 上层读写 sys/bus/驱动节点 有有没有达人知道方法

网上查了好多都不成功啊

运行到到 os.writeBytes(command + "\n");时老是出错啊 java.io.IOException: write failed: EPIPE (Broken pipe)

下面是程序
Process process = null;
DataOutputStream os = null;

        try {
            process = Runtime.getRuntime().exec("su",null,null);

            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
              e.printStackTrace();  
            return null;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                 //process.destroy();
            } catch (Exception e) {
                  e.printStackTrace();  
                return null;
            }
        }
  • 写回答

2条回答

  • jiankangshiye 2015-12-09 11:17
    关注

    写sys节点也可以的,apk要在packages/apps/目录下,用mm编译,同时要在AndroidManifest.xml配置权限

         String exe_path = "/sys/class/rfkill/rfkill0/state";
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mRefresh = (Button)findViewById(R.id.refresh);
            mRefresh.setOnClickListener(mClickListener);
    
            mMountText = (TextView)findViewById(R.id.mountText);
            mMount = (Button)findViewById(R.id.mountBtn1);
            mMount.setOnClickListener(mClickListener);
    
            try
            {
                BufferedWriter bufWriter = new BufferedWriter(new FileWriter(exe_path));
                bufWriter.write("1");  // 这儿进行的写操作
                bufWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            try {
                /*
                exe_file = new File(exe_path);  
                exe_file.setExecutable(true, true); 
                */
                Runtime runtime = Runtime.getRuntime();            
                Process process = runtime.exec("cat /sys/class/rfkill/rfkill0/state"); // 这儿进行的度操作
                InputStream is = process.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while (null != (line = br.readLine())) {
                    Log.e("########", line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!