在Android应用定制中我们可能需要通过Android应用去调用控制一些硬件设备,以实现某些特定的功能,一般会通过使用jni的方式来实现,通过jni生成.so库直接操作设备文件(通过open(),read()/write(),ioctl()函数实现设备的操作),但对于一些简单的操作,我们完全可以直接通过java对其进行读写操作,当然,直接通过java会对其效率有一定的影响,在不是很频繁操作是可以选择使用,使用java操作设备文件可以通过两种方式实现:
1)通过java的读写类实现文件的文件读写操作(文件流操作);
2)通过Process类像系统中写入shell命令,命令操作设备的文件读写操作。
接下来我们就主要介绍通过Process方式对设备进行操作。
源代码如下:
package com.android.app.cus.aging;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.util.Log;
/**
*
* @author hfcai
* @since 2015/11/30
* @see this is the ledControl class ,you can control led whether or not lighten
* according to method: control_led(String led_type, int
* on_off)、control_led_status(String led_type, boolean on_off) and
* swithLed(String led_type).at this application, we can use
* swithLed(String led_type) to control only.
*/
public class LedControl {
private static final String TAG="LedControl";
// this method is to control the led switch
public static boolean control_led(String led_type, int on_off) {
Process process = null;
DataOutputStream os = null;
try {
String command = "echo " + on_off + " > " + led_type;
process = Runtime.getRuntime().exec("sh");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
Log.d("command", command);
os.writeBytes("exit\n");
os.flush();
process.waitFor();
return true;
} catch (Exception e) {
Log.d("runtime_exception:", e.getMessage());
return false;
} finally {
try {
if (os != null)
os.close();
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// get the current status of led
public static String checkStatus(String led_type) {
String status_code = "";
try {
Process p = Runtime.getRuntime().exec("cat " + led_type);
BufferedReader in = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
status_code += line + "\n";
}
return status_code;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void control_led_status(String led_type, boolean flag) {
String status = LedControl.checkStatus(led_type);
if (status.contains("1")) {
if (!flag) {
control_led(led_type, 0);
}
} else if (status.contains("0")) {
if (flag) {
control_led(led_type, 1);
}
} else {
if (flag) {
control_led(led_type, 1);
} else {
control_led(led_type, 0);
}
}
}
public static void swithLed(String led_type) {
String status = LedControl.checkStatus(led_type);
if (status.contains("1")) {
control_led(led_type, 0);
} else {
control_led(led_type, 1);
}
}
}
如上是一个操作I/O接口led灯的操作。其中control_led(String led_type,int on_off)是控制led点亮还是熄灭的方法,通过调用Process的sh用户权限去执行shell命令,主要代码如下:
try {
String command = "echo " + on_off + " > " + led_type;
process = Runtime.getRuntime().exec("sh");//获取sh用户权限
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");//将shell命令写入文件流中
Log.d("command", command);
os.writeBytes("exit\n");
os.flush();
process.waitFor();//执行操作
return true;
} catch (Exception e) {
Log.d("runtime_exception:", e.getMessage());
return false;
} finally {
try {
if (os != null)
os.close();//关闭文件流
process.destroy();//销毁process
} catch (Exception e) {
e.printStackTrace();
}
}