上层获取ActivityManager;
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
熟悉系统服务的话,都知道getSystemService方法最后会调用到SystemServiceRegistry中;
找到添加服务的代码;
registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
new CachedServiceFetcher() {
@Override
public ActivityManager createService(ContextImpl ctx) {
return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
}});
找到ActivityManager;看一眼名词解释;
/**
* This class gives information about, and interacts
* with, activities, services, and the containing
* process.
*
* A number of the methods in this class are for
* debugging or informational purposes and they should
* not be used to affect any runtime behavior of
* your app. These methods are called out as such in
* the method level documentation.
*
* Most application developers should not have the need to
* use this class, most of whose methods are for specialized
* use cases. However, a few methods are more broadly applicable.
* For instance, {@link android.app.ActivityManager#isLowRamDevice() isLowRamDevice()}
* enables your app to detect whether it is running on a low-memory device,
* and behave accordingly.
* {@link android.app.ActivityManager#clearApplicationUserData() clearApplicationUserData()}
* is for apps with reset-data functionality.
* In some special use cases, where an app interacts with
* its Task stack, the app may use the
* {@link android.app.ActivityManager.AppTask} and
* {@link android.app.ActivityManager.RecentTaskInfo} inner
* classes. However, in general, the methods in this class should
* be used for testing and debugging purposes only.
*/
这样差不多对ActivityManager会有一个简要的了解;而后再看看其中的一些方法;
就以名词解释中提到的clearApplicationUserData为例;
/**
* Permits an application to erase its own data from disk. This is equivalent to
* the user choosing to clear the app's data from within the device settings UI. It
* erases all dynamic data associated with the app -- its private data and data in its
* private area on external storage -- but does not remove the installed application
* itself, nor any OBB files. It also revokes all runtime permissions that the app has acquired,
* clears all notifications and removes all Uri grants related to this application.
*
* @return {@code true} if the application successfully requested that the application's
* data be erased; {@code false} otherwise.
*/
public boolean clearApplicationUserData() {
return clearApplicationUserData(mContext.getPackageName(), null);
}
/**
* @hide
*/
@RequiresPermission(anyOf={Manifest.permission.CLEAR_APP_USER_DATA,
Manifest.permission.ACCESS_INSTANT_APPS})
public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) {
try {
return getService().clearApplicationUserData(packageName, false,
observer, mContext.getUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
实际的调用会通过getService拿到对应的服务;让服务去处理;
/**
* @hide
*/
public static IActivityManager getService() {
return IActivityManagerSingleton.get();
}
进入frameworks\base\core\java\android\app目录;找到IActivityManager.aidl;依然先看下名词解释;
/**
* System private API for talking with the activity manager service. This
* provides calls from the application back to the activity manager.
*
* {@hide}
*/
interface IActivityManager
在看看获取途径;很明显是个单例模式;
private static final Singleton IActivityManagerSingleton =
new Singleton() {
@Override
protected IActivityManager create() {
final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
final IActivityManager am = IActivityManager.Stub.asInterface(b);
return am;
}
};
这里贴一下单例模式的工具类;是hide的内容;
package android.util;
/**
* Singleton helper class for lazily initialization.
*
* Modeled after frameworks/base/include/utils/Singleton.h
*
* @hide
*/
public abstract class Singleton {
private T mInstance;
protected abstract T create();
public final T get() {
synchronized (this) {
if (mInstance == null) {
mInstance = create();
}
return mInstance;
}
}
}
IActivityManager是ServiceManager中的一个服务;众所周知,ServiceManager通过addService方法添加服务;顺藤摸瓜,找到添加Context.ACTIVITY_SERVICE服务的地方;在ActivityManagerService;
public void setSystemProcess() {
try {
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, /* allowIsolated= */ true,
DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PROTO);
//...
}
AMS;
public class ActivityManagerService extends IActivityManager.Stub
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback
AMS初始化;系统服务的初始化都在SystemServer;
// Activity manager runs the show.
traceBeginAndSlog("StartActivityManager");
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
//...
// Set up the Application instance for the system process and get started.
traceBeginAndSlog("SetSystemProcess");
mActivityManagerService.setSystemProcess();
真正继承SystemService的是内部类Lifecycle;
public static final class Lifecycle extends SystemService
类 | 维护的地方 | 获取途径 |
---|---|---|
ActivityManager | SystemServiceRegistry | (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); |
IActivityManager | ServiceManager | final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE); final IActivityManager am = IActivityManager.Stub.asInterface(b); |
Lifecycle | SystemServiceManager | SystemServiceManager的startService |