編輯:關於Android編程
比如#dumpsys diskstats會打印系統中內部存儲設備的使用情況。
那這個dumpsys diskstats到底是從哪裡打印存儲設備的情況的呢?
先看一下dumpsys.cpp的main函數實現
int main(int argc, char* const argv[]) { signal(SIGPIPE, SIG_IGN); spsm = defaultServiceManager(); fflush(stdout); if (sm == NULL) { ALOGE("Unable to get default service manager!"); aerr << "dumpsys: Unable to get default service manager!" << endl; return 20; } Vector services; Vector args; bool showListOnly = false; if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) { showListOnly = true; } if ((argc == 1) || showListOnly) { services = sm->listServices(); services.sort(sort_func); args.add(String16("-a")); } else { services.add(String16(argv[1])); for (int i=2; i 1) { // first print a list of the current services aout << "Currently running services:" << endl; for (size_t i=0; i service = sm->checkService(services[i]); if (service != NULL) { aout << " " << services[i] << endl; } } } if (showListOnly) { return 0; } for (size_t i=0; i service = sm->checkService(services[i]); if (service != NULL) { if (N > 1) { aout << "------------------------------------------------------------" "-------------------" << endl; aout << "DUMP OF SERVICE " << services[i] << ":" << endl; } int err = service->dump(STDOUT_FILENO, args); if (err != 0) { aerr << "Error dumping service info: (" << strerror(err) << ") " << services[i] << endl; } } else { aerr << "Can't find service: " << services[i] << endl; } } return 0; }
這裡#dumpsys diskstats命令會在dumpsys的main函數中,從所有注冊的service裡邊找到叫diskstats的,然後調用其dump()函數。那diskstats是否已經注冊了呢?
可以再SystemServer.java->startOtherServices()函數中找到
private void startOtherServices() { ... try { Slog.i(TAG, "DiskStats Service"); ServiceManager.addService("diskstats", new DiskStatsService(context)); } catch (Throwable e) { reportWtf("starting DiskStats Service", e); } } ... }
以下是DiskStatsService類的定義,可以看到已經定義了dump()函數。可以從dump()函數裡看到是怎麼打印disk信息的
public class DiskStatsService extends Binder { private static final String TAG = "DiskStatsService"; private final Context mContext; public DiskStatsService(Context context) { mContext = context; } @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); // Run a quick-and-dirty performance test: write 512 bytes byte[] junk = new byte[512]; for (int i = 0; i < junk.length; i++) junk[i] = (byte) i; // Write nonzero bytes File tmp = new File(Environment.getDataDirectory(), "system/perftest.tmp"); FileOutputStream fos = null; IOException error = null; long before = SystemClock.uptimeMillis(); try { fos = new FileOutputStream(tmp); fos.write(junk); } catch (IOException e) { error = e; } finally { try { if (fos != null) fos.close(); } catch (IOException e) {} } long after = SystemClock.uptimeMillis(); if (tmp.exists()) tmp.delete(); if (error != null) { pw.print("Test-Error: "); pw.println(error.toString()); } else { pw.print("Latency: "); pw.print(after - before); pw.println("ms [512B Data Write]"); } reportFreeSpace(Environment.getDataDirectory(), "Data", pw); reportFreeSpace(Environment.getDownloadCacheDirectory(), "Cache", pw); reportFreeSpace(new File("/system"), "System", pw); // TODO: Read /proc/yaffs and report interesting values; // add configurable (through args) performance test parameters. } 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; } } }
版本:Android stuido 2.2 windows 10操作系統網上很多都是基於早期的eclipse環境配置的,studio的很少。以下是我親測可用的一個詳細過程
一個人需要隱藏多少秘密才能巧妙地度過一生 — 倉央嘉措前言上次看到一款學習的 App,有這樣一個功能,在一個 TextView 中有一段英文,點擊英文單詞通過
使用socket與多線程實現手機聊天功能,手機客戶端的實現剛開始會出來一個登錄界面,需要我們輸入賬號,密碼,如果沒有,點擊注冊新賬號,會跳轉到注冊頁面。 注冊好的用戶會提
首先來看一下我實現的效果圖: 從”我的關注”開始,每一個條目都是一個自定義組合控件.下面,我來講解一下具體實現步驟: 一.根據