DropBoxManagerService
Q:\1103\frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.java的addErrorToDropBox方法
public void addErrorToDropBox(String eventType,\\error的类型(crash anr wtf等等)
ProcessRecord process,
String processName,
String activityShortComponentName,
String parentShortComponentName, \\跟此error相关的activity
ProcessRecord parentProcess,
String subject, \\与error有关的相关行
final String report,
final File dataFile,
final ApplicationErrorReport.CrashInfo crashInfo)\\error方法调用堆栈
if (ServiceManager.getService(Context.DROPBOX_SERVICE) == null) return;
final DropBoxManager dbox = mContext.getSystemService(DropBoxManager.class);
//获取DropBoxManagerService的管理对象DropBoxManager
......
final StringBuilder sb = new StringBuilder(1024);
//创建一个大小为1024的StringBuilder对象拼接信息
......
//创建一个线程进行数据的读写(防止在AMS中发生IO阻塞)
Thread worker = new Thread("Error dump: " + dropboxTag) {
@Override
public void run() {
if (report != null) {
sb.append(report);
}
......
dbox.addText(dropboxTag, sb.toString());//把信息传递给DropBoxManager
.....
}};
//进程不为空时,线程运行保存信息
if (process == null) {
final int oldMask = StrictMode.allowThreadDiskWritesMask();
try {
worker.run();
} finally {
StrictMode.setThreadPolicyMask(oldMask);
}
} else {
worker.start();
}
调用DropBoxManager的addText,进而调用到DropBoxManagerService服务
Q:\1103\frameworks\base\core\java\android\os\DropBoxManager.java
public void addText(String tag, String data) {
try {
mService.add(new Entry(tag, 0, data)); //将信息包装成一个Entry对象
} catch (RemoteException e) {
if (e instanceof TransactionTooLargeException
&& mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
Log.e(TAG, "App sent too much data, so it was ignored", e);
return;
}
throw e.rethrowFromSystemServer();
}
}
DropBoxManagerService创建
Q:\1103\frameworks\base\services\java\com\android\server\SystemServer.java
在startOtherServices方法中
t.traceBegin("StartDropBoxManager");
mSystemServiceManager.startService(DropBoxManagerService.class);
t.traceEnd();
Q:\1103\frameworks\base\services\core\java\com\android\server\DropBoxManagerService.java
public DropBoxManagerService(final Context context) {
this(context, new File("/data/system/dropbox"), FgThread.get().getLooper());
}
//文件保存路径
public DropBoxManagerService(final Context context, File path, Looper looper) {
super(context);
mDropBoxDir = path;
mContentResolver = getContext().getContentResolver();
mHandler = new DropBoxManagerBroadcastHandler(looper);
}
public void add(DropBoxManager.Entry entry) {
File temp = null;
InputStream input = null;
OutputStream output = null;
//创建InputStream OutputStream File
......
input = entry.getInputStream();
//从传入的Entry对象获取信息
temp = new File(mDropBoxDir, "drop" + Thread.currentThread().getId() + ".tmp");
//创建文件
.....
while循环output.write(buffer, 0, read); 写入数据