public void produceSystemLog() {
IBinder c = ServiceManager.getService("cpuinfo");
if (c == null) {
return;
}
FileOutputStream fileOutputStream = null;
try {
String file = context.getFilesDir().getAbsolutePath() + "/" + "cup";
fileOutputStream = new FileOutputStream(file);
FileDescriptor fileDescriptor = fileOutputStream.getFD();
c.dump(fileDescriptor, null);
} catch (RemoteException | IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
其核心思想有这几点:
- 使用ServiceManager的getService获取对应service的Binder代理IBinder
- 定义FileDescriptor指向目标文件
- 调用目标service的dump接口,把dump信息输出到目标文件
另外:
由于ServiceManager是@hide类型的,所以一般应用无法访问,可以通过反射的方法获取到对应service的Binder代理,但是dump需要用到下面的权限:
<uses-permission android:name="android.permission.DUMP"/>
而这个权限要求只有系统应用才能使用,所以普通应用还是无法dump系统信息。