简介
我们知道AMS是Android四大组件的管理核心,通过AMS的管理,四大四大组件的创建和销毁也就不是问题,但是一般情况下我们了解到四大组件如何使用,并不需要我们关心系统如何管理这些组件,所以我们就从AMS的创建谈起,说说Android系统如何进行管理这些组件的。
我们从systemserver开始说起,至于systemserver如何创建的,我们这一节并不关心,话不多说看下文。
SystemServer.java
public static void main(String[] args) {
new SystemServer().run();//[1.0]
}
1.0 SystemServer().run
private void run() {
//设置线程优先级
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND);
Looper.prepareMainLooper();
//[1.1]创建systemserver进程的ActivityThread和SystemContext
createSystemContext();
//创建SystemService的创建,启动和生命周期,多用户切换
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
//启动服务
try {
startBootstrapServices();//[1.2]
startCoreServices();//
startOtherServices();//[1.3]
SystemServerInitThreadPool.shutdown();
} catch (Throwable ex) {
throw ex;
} finally {
}
}
1.2 SystemServer.createSystemContext()
private void createSystemContext() {
}
1.2 SystemServer.startBootstrapServices()
在创建核心服务的方法中,第一个就创建了AMS,可见AMS的重要性
private void startBootstrapServices() {
mActivityManagerService = mSystemServiceManager.startService(ActivityManagerService.Lifecycle.class).getService();//[*]
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);//设置SystemServiceManager,SystemServiceManager在SystemServer().run中创建过了
mActivityManagerService.initPowerManagement();//初始化电源服务
mActivityManagerService.setSystemProcess();//[2.3]添加一下比较重要的Binder服务
}
这里SystemServiceManager的startService()方法步骤:
- 通过XX.class得到类名
- 通过类名得到构造
- 调用构造生成对象
- 然后添加到ArrayList<SystemService> mServices = new ArrayList<SystemService>();的列表中
- 然后调用mService的onStart()方法
在这个过程中牵扯到两个核心
- 调用对象的构造[2.1]
- 调用对象的onStart方法[2.2]
所以这里牵扯到调用AMS的onStart()
我们索性将2.1和2.2都归到2中
2.1+2.2 AMS.Lifecycle.class
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
public Lifecycle(Context context) {
super(context);
mService = new ActivityManagerService(context);
}
@Override
public void onStart() {
mService.start();
}
public ActivityManagerService getService() {
return mService;
}
}
所以我们看见,在Lifecycle的构造中生成了AMS的实力对象,在onStart()中调用了AMS的start(),在getService()中可以得到AMS的实力对象
2.3 AMS.setSystemProcess()
public void setSystemProcess() {
try {
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
ServiceManager.addService("meminfo", new MemBinder(this));
ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
ServiceManager.addService("dbinfo", new DbBinder(this));
if (MONITOR_CPU_USAGE) {
ServiceManager.addService("cpuinfo", new CpuBinder(this));
}
ServiceManager.addService("permission", new PermissionController(this));
ServiceManager.addService("processinfo", new ProcessInfoService(this));
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo("android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
synchronized (this) {
ProcessRecord app = newProcessRecordLocked(info, info.processName, false, 0);
app.persistent = true;
app.pid = MY_PID;
app.maxAdj = ProcessList.SYSTEM_ADJ;
app.makeActive(mSystemThread.getApplicationThread(), mProcessStats);
synchronized (mPidsSelfLocked) {
mPidsSelfLocked.put(app.pid, app);
}
updateLruProcessLocked(app, false, null);
updateOomAdjLocked();
}
} catch (PackageManager.NameNotFoundException e) {
}
}
我们在解释SystemServer要通过AMS添加的服务之前我们先看一看addService这个方法的实现
ServiceManager.addService
public static void addService(String name, IBinder service) {
try {
getIServiceManager().addService(name, service, false);
} catch (RemoteException e) {
Log.e(TAG, "error in addService", e);
}
}
private static IServiceManager getIServiceManager() {
if (sServiceManager != null) {
return sServiceManager;
}
// Find the service manager
sServiceManager = ServiceManagerNative.asInterface(BinderInternal.getContextObject());
return sServiceManager;
}
到这里解释一下,我们通过addService方法内部的探寻,看到ServiceManager的方法单纯只是代理别人的
addService方法,这里的别人是ServiceManagerProxy,ServiceManagerProxy又是通过Binder的方式传入
的参数是BinderInternal.getContextObject(),
我们看到描述信息:
/**
* Return the global "context object" of the system. This is usually
* an implementation of IServiceManager, which you can use to find
* other services.
*/
BinderInternal.getContextObject()
我们对应到c++的getContextObject()
static jobject android_os_BinderInternal_getContextObject(JNIEnv* env, jobject clazz)
{
sp<IBinder> b = ProcessState::self()->getContextObject(NULL);//返回的是new BpBinder(0)
return javaObjectForIBinder(env, b);//这个方法是JNI的应用,返回的是BinderProxy
}
这里涉及的Binder通信我们后面会专门有文章讲解,现在我们知道返回的是BpBinder(0),这个对象就是handle=0的对象。也就是service_manager对象的代理。servicemanager是android的服务大管家。
所以小节这一段内容,就是将一些Binder服务添加到servicemanager中。
整理一下SystemServer要通过AMS添加的服务有那些:
- ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);//ACTIVITY_SERVICE是activity,代表的是AMS
- ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);//procstats代表ProcessStatsService是进程统计
- ServiceManager.addService("meminfo", new MemBinder(this));//meminfo,用于检测系统内存情况
- ServiceManager.addService("gfxinfo", new GraphicsBinder(this));//gfxinfo,图像信息
- ServiceManager.addService("dbinfo", new DbBinder(this));//dbinfo,数据库服务
- ServiceManager.addService("cpuinfo", new CpuBinder(this));//cpuinfo,cpu信息
- ServiceManager.addService("permission", new PermissionController(this));//权限管理
- ServiceManager.addService("processinfo", new ProcessInfoService(this));//得到进程信息
我们在这些类中将会看到对应的 dump 方法,所以我们就可以用命令进行操作(我们在最后总结):
可通过dumpsys <服务名>命令。比如查看CPU信息命令dumpsys cpuinfo。
其次setSystemProcess方法中还创建了ApplicationInfo看代码:
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo( "android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
//其中mContext就是我们在创建AMS传进去的也就是通过SystemServer中createSystemContext()方法创建出来的
private void createSystemContext() {
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
}
这段代码就是创建AT对象,然后通过AT.getSystemContext()方法得到Context对象
public ContextImpl getSystemContext() {
synchronized (this) {
if (mSystemContext == null) {
mSystemContext = ContextImpl.createSystemContext(this);
}
return mSystemContext;
}
}
static ContextImpl createSystemContext(ActivityThread mainThread) {
LoadedApk packageInfo = new LoadedApk(mainThread);
ContextImpl context = new ContextImpl(null, mainThread,
packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
context.mResourcesManager.getDisplayMetrics());
return context;
}
所以我们目前就知道,原来是在SystemServer中通过getSystemContext()方法得到ContextImpl实力对象
我们在ContextImpl中通过LoadeApl对象调用其方法getApplicationInfo()得到ApplicationInfo,这个对象是在LoadedApk的构造中创建的new ApplicationInfo();
那new ApplicationInfo();这个对象是干什么的呢?
它可以从AndroidManifest.xml里的<application>标签里的各个属性值。
这里我们在大概说一说getApplicationInfo()这个方法,这个方法根源来自于PMS,在PMS中我们找到对应代码
@Override
public ApplicationInfo getApplicationInfo(String packageName, int flags, int userId) {
if (!sUserManager.exists(userId)) return null;
flags = updateFlagsForApplication(flags, userId, packageName);
enforceCrossUserPermission(Binder.getCallingUid(), userId,
false /* requireFullPermission */, false /* checkShell */, "get application info");
// writer
synchronized (mPackages) {
PackageParser.Package p = mPackages.get(packageName);
if (DEBUG_PACKAGE_INFO) Log.v(
TAG, "getApplicationInfo " + packageName
+ ": " + p);
if (p != null) {
PackageSetting ps = mSettings.mPackages.get(packageName);
if (ps == null) return null;
// Note: isEnabledLP() does not apply here - always return info
// MIUI MOD: START
// return PackageParser.generateApplicationInfo(
// p, flags, ps.readUserState(userId), userId);
ApplicationInfo info = PackageParser.generateApplicationInfo(
p, flags, ps.readUserState(userId), userId);
if (info == null && miui.securityspace.XSpaceUserHandle.isXSpaceUserCalling()) {
flags |= PackageManager.GET_UNINSTALLED_PACKAGES;
return generateApplicationInfoFromSettingsLPw(packageName, flags, userId);
} else {
return info;
}
// END
}
if ("android".equals(packageName)||"system".equals(packageName)) {
return mAndroidApplication;
}
if ((flags & MATCH_UNINSTALLED_PACKAGES) != 0) {
return generateApplicationInfoFromSettingsLPw(packageName, flags, userId);
}
}
return null;
}
我们传递进入的参数:
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo("android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
所以对应到return mAndroidApplication;这个直接new ApplicationInfo()对象
在setSystemProcess方法最后生成ProcessRecord对象。并更新adj的值。
所以总结AMS.setSystemProcess(),就是创建一些Binder服务,并创建ApplicationInfo,还创建了ProcessRecord对象并更新adj目的是将SystemServer添加到AMS中管理
AMS构造
我们上面说过在mSystemServiceManager.startService(ActivityManagerService.Lifecycle.class).getService();创建了AMS的实力对象并返回我们现在看看构造函数干了什么
public ActivityManagerService(Context systemContext) {
mContext = systemContext;
mFactoryTest = FactoryTest.getMode();//默认为FACTORY_TEST_OFF
mSystemThread = ActivityThread.currentActivityThread();
//创建名为"ActivityManager"的前台线程,并获取mHandler
mHandlerThread = new ServiceThread(TAG, android.os.Process.THREAD_PRIORITY_FOREGROUND, false);
mHandlerThread.start();
mHandler = new MainHandler(mHandlerThread.getLooper());
//通过UiThread类,创建名为"android.ui"的线程
mUiHandler = new UiHandler();
//前台广播接收器,在运行超过10s将放弃执行
mFgBroadcastQueue = new BroadcastQueue(this, mHandler,
"foreground", BROADCAST_FG_TIMEOUT, false);
//后台广播接收器,在运行超过60s将放弃执行
mBgBroadcastQueue = new BroadcastQueue(this, mHandler,
"background", BROADCAST_BG_TIMEOUT, true);
mBroadcastQueues[0] = mFgBroadcastQueue;
mBroadcastQueues[1] = mBgBroadcastQueue;
//创建ActiveServices,其中非低内存手机mMaxStartingBackground为8
mServices = new ActiveServices(this);
mProviderMap = new ProviderMap(this);
//创建目录/data/system
File dataDir = Environment.getDataDirectory();
File systemDir = new File(dataDir, "system");
systemDir.mkdirs();
//创建服务BatteryStatsService
mBatteryStatsService = new BatteryStatsService(systemDir, mHandler);
mBatteryStatsService.getActiveStatistics().readLocked();
...
//创建进程统计服务,信息保存在目录/data/system/procstats,
mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
mAppOpsService = new AppOpsService(new File(systemDir, "appops.xml"), mHandler);
mGrantFile = new AtomicFile(new File(systemDir, "urigrants.xml"));
// User 0是第一个,也是唯一的一个开机过程中运行的用户
mStartedUsers.put(UserHandle.USER_OWNER, new UserState(UserHandle.OWNER, true));
mUserLru.add(UserHandle.USER_OWNER);
updateStartedUserArrayLocked();
...
//CPU使用情况的追踪器执行初始化
mProcessCpuTracker.init();
...
mRecentTasks = new RecentTasks(this);
// 创建ActivityStackSupervisor对象
mStackSupervisor = new ActivityStackSupervisor(this, mRecentTasks);
mTaskPersister = new TaskPersister(systemDir, mStackSupervisor, mRecentTasks);
//创建名为"CpuTracker"的线程
mProcessCpuThread = new Thread("CpuTracker") {
public void run() {
while (true) {
try {
try {
synchronized(this) {
final long now = SystemClock.uptimeMillis();
long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now;
long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now;
if (nextWriteDelay < nextCpuDelay) {
nextCpuDelay = nextWriteDelay;
}
if (nextCpuDelay > 0) {
mProcessCpuMutexFree.set(true);
this.wait(nextCpuDelay);
}
}
} catch (InterruptedException e) {
}
updateCpuStatsNow(); //更新CPU状态
} catch (Exception e) {
}
}
}
};
...
}
这个过程创建了三个线程
- 名为"ActivityManager"的前台线程并获取mHandler
- 名为"android.ui"的线程
- 名为"CpuTracker"的线程
同时创建了很多核心的对象
- 系统Context 和 ActivityThread (将systemserver进程作为应用进程管理)
- AMS工作的线程和Handler,处理显示相关的UiHandler ---》知识点HandlerThread和Handler
- 广播队列BroadcastQueue初始化:前台广播队列和后台广播队列
- Service 和 Provider 管理
- 系统数据存放目录:/data/system/
- 多用户管理
- 最近任务,Activity,Task管理
- 创建一个新线程,用于监控和定时更新系统CPU信息,30分钟更新一次CPU和电池信息
- 加入Watchdog监控起来
构造方法中涉及到四大组件的初始化
- Broadcast --> BroadcastQueue
- Provider --> ProviderMap
- Service --> ActiveServices
- Activity --> ActivityStackSupervisor
1.3 SystemServer.startOtherServices()
private void startOtherServices() {
mActivityManagerService.installSystemProviders();//会安装Android系统的provider通过mSystemThread.installSystemProviders(providers);
...
mActivityManagerService.systemReady(new Runnable() {//[2.4]
public void run() {
mSystemServiceManager.startBootPhase(
SystemService.PHASE_ACTIVITY_MANAGER_READY);
mActivityManagerService.startObservingNativeCrashes();
//启动WebView
WebViewFactory.prepareWebViewInSystemServer();
startSystemUi(context);
// 执行一系列服务的systemReady方法
networkScoreF.systemReady();
networkManagementF.systemReady();
networkStatsF.systemReady();
networkPolicyF.systemReady();
connectivityF.systemReady();
audioServiceF.systemReady();
Watchdog.getInstance().start(); //Watchdog开始工作
//phase600
mSystemServiceManager.startBootPhase(
SystemService.PHASE_THIRD_PARTY_APPS_CAN_START);
//执行一系列服务的systemRunning方法
wallpaper.systemRunning();
inputMethodManager.systemRunning(statusBarF);
location.systemRunning();
countryDetector.systemRunning();
networkTimeUpdater.systemRunning();
commonTimeMgmtService.systemRunning();
textServiceManagerService.systemRunning();
assetAtlasService.systemRunning();
inputManager.systemRunning();
telephonyRegistry.systemRunning();
mediaRouter.systemRunning();
mmsService.systemRunning();
}
}
}
- 启动WebView,并且创建进程,这个是zygote正式创建的第一个进程
- 启动systemui服务
- 那些startBootPhase都对应的不同的阶段,根据阶段值进行回调的功能的区分
public void systemReady(final Runnable goingCallback) {
before goingCallback;
goingCallback.run();//run方法是在中间运行的
after goingCallback;
}
2.4 AMS.systemReady()
public void systemReady(final Runnable goingCallback) {
synchronized(this) {
if (mSystemReady) {
goingCallback.run();
}
……
// 1.升级相关处理:发送PRE_BOOT_COMPLETED广播 等待升级处理完成才能继续
// Check to see if there are any update receivers to run.
if (!mDidUpdate) {
// 等待升级完成,否则直接返回
if (mWaitingUpdate) {
return;
}
// 发送PRE_BOOT_COMPLETED广播
final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>();
mWaitingUpdate = deliverPreBootCompleted(new Runnable() {
// 等待所有接收PRE_BOOT_COMPLETED广播者处理完毕
public void run() {
synchronized (ActivityManagerService.this) {
mDidUpdate = true;
}
showBootMessage(mContext.getText(
R.string.android_upgrading_complete),
false);
// 将系统版本号和处理过的广播写入文件:/data/system/called_pre_boots.dat文件
writeLastDonePreBootReceivers(doneReceivers);
// 继续systemReady流程
systemReady(goingCallback);
}
}, doneReceivers, UserHandle.USER_OWNER);
if (mWaitingUpdate) {
return;
}
mDidUpdate = true;
}
mSystemReady = true;
}
// 2. 收集已经启动的进程并杀死,除过persistent常驻进程
////非persistent进程,加入procsToKill
ArrayList<ProcessRecord> procsToKill = null;
synchronized(mPidsSelfLocked) {
for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
ProcessRecord proc = mPidsSelfLocked.valueAt(i);
if (!isAllowedWhileBooting(proc.info)){
if (procsToKill == null) {
procsToKill = new ArrayList<ProcessRecord>();
}
procsToKill.add(proc);
}
}
}
synchronized(this) {
//杀掉procsToKill中的进程, 杀掉进程且不允许重启
if (procsToKill != null) {
for (int i=procsToKill.size()-1; i>=0; i--) {
ProcessRecord proc = procsToKill.get(i);
Slog.i(TAG, "Removing system update proc: " + proc);
removeProcessLocked(proc, true, false, "system update done");
}
}
// Now that we have cleaned up any update processes, we
// are ready to start launching real processes and know that
// we won't trample on them any more.
mProcessesReady = true;
}
// 3.系统准备好后回调传入的Runnable:
if (goingCallback != null) goingCallback.run();
// 4. 发送账户启动的广播,涉及多用户
long ident = Binder.clearCallingIdentity();
Intent intent = new Intent(Intent.ACTION_USER_STARTED);
broadcastIntentLocked(intent);
intent = new Intent(Intent.ACTION_USER_STARTING);
broadcastIntentLocked(intent);
Binder.restoreCallingIdentity(ident);
// 5. 启动桌面Home Activity
// 启动桌面Activity 【见小节3.3.2】
startHomeActivityLocked(mCurrentUserId, "systemReady");
...
long ident = Binder.clearCallingIdentity();
try {
//system发送广播USER_STARTED
Intent intent = new Intent(Intent.ACTION_USER_STARTED);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
| Intent.FLAG_RECEIVER_FOREGROUND);
intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId);
broadcastIntentLocked(...);
//system发送广播USER_STARTING
intent = new Intent(Intent.ACTION_USER_STARTING);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId);
broadcastIntentLocked(...);
} finally {
Binder.restoreCallingIdentity(ident);
}
mStackSupervisor.resumeTopActivitiesLocked();
sendUserSwitchBroadcastsLocked(-1, mCurrentUserId);
}
小节:
- 向PRE_BOOT_COMPLETED的接收者发送广播;
- 杀掉procsToKill中的进程, 杀掉进程且不允许重启;
- 此时,系统和进程都处于ready状态;
- 回调所有SystemService的onStartUser()方法
- 启动persistent进程;
- 启动home Activity;
- 发送广播USER_STARTED和USER_STARTING;
- 恢复栈顶Activity;
- 发送广播USER_SWITCHED;
这里我们需要注意的是:
startSystemUi
static final void startSystemUi(Context context) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.systemui",
"com.android.systemui.SystemUIService"));
context.startServiceAsUser(intent, UserHandle.OWNER);
}
boolean startHomeActivityLocked(int userId, String reason) {
//home intent有CATEGORY_HOME
Intent intent = getHomeIntent();
ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
if (aInfo != null) {
intent.setComponent(new ComponentName(
aInfo.applicationInfo.packageName, aInfo.name));
aInfo = new ActivityInfo(aInfo);
aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
ProcessRecord app = getProcessRecordLocked(aInfo.processName,
aInfo.applicationInfo.uid, true);
if (app == null || app.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
//启动桌面Activity
mStackSupervisor.startHomeActivity(intent, aInfo, reason);
}
}
return true;
}
所以我们这里也能清楚一个流程,就是桌面也是一个app,这个app在AMS创建之后在SystsemServer.startOtherServices()中调用AMS.systemReady()中创建并启动Activity。
这里要注意systemReady方法中:
- removeProcessLocked(proc, true, false, "system update done");//杀掉所有非persistent进程
- addAppLocked(info, false, null); //启动所有的persistent进程
- startHomeActivityLocked(mCurrentUserId, "systemReady"); //启动桌面
- cleanUpApplicationRecordLock() //启动需要restart进程,前提是进程已创建;
- attachApplicationLocked() //绑定Bind死亡通告失败,前台同样是进程要已创建。
实用技巧总结:
AMS.setSystemProcess()过程向servicemanager注册了如下这个binder服务
服务名 | 类名 | 功能 |
---|---|---|
activity | ActivityManagerService | AMS |
procstats | ProcessStatsService | 进程统计 |
meminfo | MemBinder | 内存 |
gfxinfo | GraphicsBinder | 图像信息 |
dbinfo | DbBinder | 数据库 |
cpuinfo | CpuBinder | CPU |
permission | PermissionController | 权限 |
processinfo | ProcessInfoService | 进程服务 |
usagestats | UsageStatsService | 应用的使用情况 |
其中usagestats并没有在setSystemProcess中创建,而是在其他地方创建的,具体那里,没找到...
我们顺便总结一下可以dumpsys的服务吧,可好玩是不是哈哈
使用adb shell dumpsys dbinfo
就会执行DbBinder.dump()
:
static class DbBinder extends Binder {
ActivityManagerService mActivityManagerService;
DbBinder(ActivityManagerService activityManagerService) {
mActivityManagerService = activityManagerService;
}
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
!= PackageManager.PERMISSION_GRANTED) {
pw.println("Permission Denial: can't dump dbinfo from from pid="
+ Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
+ " without permission " + android.Manifest.permission.DUMP);
return;
}
mActivityManagerService.dumpDbInfo(fd, pw, args);
}
}
mActivityManagerService.dumpDbInfo(fd, pw, args);
@Override
public void dumpDbInfo(final FileDescriptor fd, final String[] args) {
if (mSystemThread) {
// Ensure this invocation is asynchronous to prevent writer waiting if buffer cannot
// be consumed. But it must duplicate the file descriptor first, since caller might
// be closing it.
final ParcelFileDescriptor dup;
try {
dup = ParcelFileDescriptor.dup(fd);
} catch (IOException e) {
Log.w(TAG, "Could not dup FD " + fd.getInt$());
return;
}
AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
@Override
public void run() {
try {
dumpDatabaseInfo(dup.getFileDescriptor(), args);
} finally {
IoUtils.closeQuietly(dup);
}
}
});
} else {
dumpDatabaseInfo(fd, args);
}
}
public static ParcelFileDescriptor dup(FileDescriptor orig) throws IOException {
try {
final FileDescriptor fd = Os.dup(orig);
return new ParcelFileDescriptor(fd);
} catch (ErrnoException e) {
throw e.rethrowAsIOException();
}
}
基本都在Context中定义
- activity
- procstats
- meminfo
- gfxinfo
- dbinfo
- cpuinfo
- permission
- processinfo
- appops
- batterystats
- content
- package
- isub
- user
- telephony.registry
- account
- vibrator
- window
- diskstats
- ...
有很多很多,我把那些名字直接用字符串的形式写入的整理到上面了,也有一些是Context中的,至于Context中的用到的在里面查
比如我们现在看数据库的dump信息
使用
adb dumpsys dbinfo