因为vitamio开源框架只适合于ARMv6以及以上的CPU,所以为了防止程序崩溃,在播放程序运行之前需要检测一下手机的CPU看是否支持。
这就用到了这个方法。
首先看一下CMDExecute类:
此类可以用来读取传入的文件。
package com.example.cpuinfotest; import java.io.File; import java.io.IOException; import java.io.InputStream; public class CMDExecute { public synchronized String run(String[] args, String workdirectory) throws IOException { String result = null; try { ProcessBuilder builder = new ProcessBuilder(args); InputStream in = null; //设置一个路径 if(workdirectory != null){ builder.directory(new File(workdirectory)); builder.redirectErrorStream(true); Process process = builder.start(); in = process.getInputStream(); byte[] re = new byte[1024]; while(in.read(re) != -1){ result = result + new String(re); } if(in != null){ in.close(); } } } catch (Exception e) { e.printStackTrace(); } return result; } }
然后是MainActivity:
调用fetch_cpu_info() 将系统日志文件的路径传进去,返回的result就是手机CPU的信息啦。
如图:
CPUarchitecture() 这个方法就是获取CPU架构号的方法,将fetch_cpu_info()返回的结果传入这个方法,即得到我们需要的数字。
我的手机的CPU是ARMv7的,所以此方法输出的结果即为" 7 "。于是我们就可以根据这个来判断手机支持不支持vitamio了。
package com.example.cpuinfotest; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; private String a; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); a = fetch_cpu_info(); System.out.println("==========================="+CPUarchitecture(a)); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); tv.setText(CPUarchitecture(a)); } private String fetch_cpu_info() { String result = null; CMDExecute cmdexe = new CMDExecute(); try { String[] args = { "/system/bin/cat", "/proc/cpuinfo" }; result = cmdexe.run(args, "/system/bin/"); Log.i(result, result += result); } catch (Exception e) { e.printStackTrace(); } return result; } // 获得CPU架构数字。。。 private String CPUarchitecture(String result) { String s = result; int n_pos; n_pos = s.indexOf("("); String str_temp = new String(); str_temp = s.substring(n_pos + 1, s.length()); n_pos = str_temp.indexOf(")"); str_temp = str_temp.substring(0, n_pos); n_pos = str_temp.indexOf("v"); str_temp = str_temp.substring(n_pos + 1, str_temp.length() - 1); return str_temp; } }
这是我根据搜索到的资料整理的,希望能对其他人有所帮助吧。