大家都知道现在google越来越现在我们对权限的操作,尤其是要通过认证的项目,一不小心就会引起测试fail,那么问题来了,如果不添加权限,那么怎么去操作那些节点呢?
PowerManagerService 提供Android系统的电源管理服务,那么我们也可以在这里进行一些对节点的操作;
首先,在frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java进行对节点的具体操作,
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
-------------------------------------------------------------------
+ @Override // Binder call
+ public void write_proc(String path, String value){
+ File file2 = new File(path);
+ FileOutputStream fos2;
+ try {
+ fos2 = new FileOutputStream(file2);
+ fos2.write(value.getBytes());
+ fos2.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override // Binder call
+ public String read_proc(String path){
+ FileReader reader = null;
+ char[] buf = new char[16];;
+ try {
+ reader = new FileReader(path);
+ int n = reader.read(buf);
+ } catch (IOException ex) {
+
+ }finally {
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (IOException ex) {
+ }
+ }
+ }
+ if(buf != null)
+ return String.valueOf(buf);
+ else
+ return null;
+ }
+
@Override // Binder call
public void boostScreenBrightness(long eventTime) {
if (eventTime > SystemClock.uptimeMillis()) {
其次,我们在frameworks/base/core/java/android/os/IPowerManager.aidl这个方法中去定义我们上层操作节点的方法
// sets the attention light (used by phone app only)
void setAttentionLight(boolean on, int color);
+
+ void write_proc(String path, String value);
+ String read_proc(String path);
最后在frameworks/base/core/java/android/os/PowerManager.java中进行对PowerManagerService.java中新添加的操作节点方法的调用
+ /**
+ * @hide
+ */
+ public void write_proc(String path, String value){
+ try {
+ mService.write_proc(path, value);
+ } catch (RemoteException e) {
+ throw e.rethrowFromSystemServer();
+ }
+ }
+ /**
+ * @hide
+ */
+ public String read_proc(String path){
+ try {
+ return mService.read_proc(path);
+ } catch (RemoteException e) {
+ throw e.rethrowFromSystemServer();
+ }
+ }
+
/**
* A wake lock is a mechanism to indicate that your application needs
* to have the device stay on.
到了这一步我们就已经完成了对在framerwork层添加节点的操作方法了,那么我们就可以在具体的地方对其进行调用
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
pm.write_proc("节点路径",“值(string类型)”);
pm.read_proc("节点路径");