在ServerThread的run函数中,注册了系统大部分的Service,有兴趣的同学可以看看这部分代码
这部分代码有点长(大概800行),因为它一直重复这建立各种Service,然后注册,输出log
比如下面的WifiP2pService
try {
Slog.i(TAG, "Wi-Fi P2pService");
wifiP2p = new WifiP2pService(context);
ServiceManager.addService(Context.WIFI_P2P_SERVICE, wifiP2p);
} catch (Throwable e) {
reportWtf("starting Wi-Fi P2pService", e);
}
其中WIFI_P2P_SERVICE就是服务的名字
public static final String WIFI_P2P_SERVICE = "wifip2p";
当执行dumpsys wifip2p的时候,并没有任何输出,为什么呢?我们看看WifiP2pService的代码
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
if (mContext.checkCallingOrSelfPermission(Android.Manifest.permission.DUMP)
!= PackageManager.PERMISSION_GRANTED) {
pw.println("Permission Denial: can't dump WifiP2pService from from pid="
+ Binder.getCallingPid()
+ ", uid=" + Binder.getCallingUid());
return;
}
}
现在似乎可以理解为什么dumpsys wifip2p的时候没有输出了
我们再来看一个简单的例子
# dumpsys diskstats
Latency: 5ms [512B Data Write]
Data-Free: 362888K / 1161104K total = 31% free
Cache-Free: 116756K / 120900K total = 96% free
System-Free: 133036K / 806284K total = 16% free
ServiceManager.addService("diskstats", new DiskStatsService(context));
这回大家知道去哪看源码了吧,下面是DiskStatsService的dump函数
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
……
reportFreeSpace(Environment.getDataDirectory(), "Data", pw);
reportFreeSpace(Environment.getDownloadCacheDirectory(), "Cache", pw);
reportFreeSpace(new File("/system"), "System", pw);
}
private void reportFreeSpace(File path, String name, PrintWriter pw) {
try {
StatFs statfs = new StatFs(path.getPath());
long bsize = statfs.getBlockSize();
long avail = statfs.getAvailableBlocks();
long total = statfs.getBlockCount();
if (bsize <= 0 || total <= 0) {
throw new IllegalArgumentException(
"Invalid stat: bsize=" + bsize + " avail=" + avail + " total=" + total);
}
pw.print(name);
pw.print("-Free: ");
pw.print(avail * bsize / 1024);
pw.print("K / ");
pw.print(total * bsize / 1024);
pw.print("K total = ");
pw.print(avail * 100 / total);
pw.println("% free");
} catch (IllegalArgumentException e) {
pw.print(name);
pw.print("-Error: ");
pw.println(e.toString());
return;
}
}