Android systemserver分析ThrottleService 介绍

System Server是Android系统的核心,他在Dalvik虚拟机启动后立即开始初始化和运行。其它的系统服务在System Server进程的环境中运行。/base/services/java/com/android/server/SystemServer.java

Java代码  

  1. /**
  2. * This method is called from Zygote to initialize the system. This will cause the native
  3. * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
  4. * up into init2() to start the Android services.
  5. */
  6. native   public   static   void  init1(String[] args);
  7. public   static   void  main(String[] args) {
  8. if  (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
  9. // If a device‘s clock is before 1970 (before 0), a lot of
  10. // APIs crash dealing with negative numbers, notably
  11. // java.io.File#setLastModified, so instead we fake it and
  12. // hope that time from cell towers or NTP fixes it
  13. // shortly.
  14. Slog.w(TAG, "System clock is before 1970; setting to 1970." );
  15. SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
  16. }
  17. if  (SamplingProfilerIntegration.isEnabled()) {
  18. SamplingProfilerIntegration.start();
  19. timer = new  Timer();
  20. timer.schedule(new  TimerTask() {
  21. @Override
  22. public   void  run() {
  23. SamplingProfilerIntegration.writeSnapshot("system_server" );
  24. }
  25. }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
  26. }
  27. // The system server has to run all of the time, so it needs to be
  28. // as efficient as possible with its memory usage.
  29. VMRuntime.getRuntime().setTargetHeapUtilization(0 .8f);
  30. System.loadLibrary("android_servers" );
  31. init1(args);
  32. }
  33. public   static   final   void  init2() {
  34. Slog.i(TAG, "Entered the Android system server!" );
  35. Thread thr = new  ServerThread();
  36. thr.setName("android.server.ServerThread" );
  37. thr.start();
  38. }

在main函数中,首先检查系统时间设置和SamplingProfiler。然后加载一个叫android_servers的本地库,他提供本 地方法的接口(源程序在framework/base/services/jni/目录中)。然后调用本地方法设置服务。具体执行设置的代码在 frameworks/base/cmds/system_server/library/system_init.cpp中。

C代码  

  1. extern   "C"  status_t system_init()
  2. {
  3. LOGI("Entered system_init()" );
  4. sp<ProcessState> proc(ProcessState::self());
  5. sp<IServiceManager> sm = defaultServiceManager();
  6. LOGI("ServiceManager: %p\n" , sm.get());
  7. sp<GrimReaper> grim = new  GrimReaper();
  8. sm->asBinder()->linkToDeath(grim, grim.get(), 0);
  9. char  propBuf[PROPERTY_VALUE_MAX];
  10. property_get("system_init.startsurfaceflinger" , propBuf,  "1" );
  11. if  (strcmp(propBuf,  "1" ) == 0) {
  12. // Start the SurfaceFlinger
  13. SurfaceFlinger::instantiate();
  14. }
  15. // Start the sensor service
  16. SensorService::instantiate();
  17. // On the simulator, audioflinger et al don‘t get started the
  18. // same way as on the device, and we need to start them here
  19. if  (!proc->supportsProcesses()) {
  20. // Start the AudioFlinger
  21. AudioFlinger::instantiate();
  22. // Start the media playback service
  23. MediaPlayerService::instantiate();
  24. // Start the camera service
  25. CameraService::instantiate();
  26. // Start the audio policy service
  27. AudioPolicyService::instantiate();
  28. }
  29. // And now start the Android runtime.  We have to do this bit
  30. // of nastiness because the Android runtime initialization requires
  31. // some of the core system services to already be started.
  32. // All other servers should just start the Android runtime at
  33. // the beginning of their processes‘s main(), before calling
  34. // the init function.
  35. LOGI("System server: starting Android runtime.\n" );
  36. AndroidRuntime* runtime = AndroidRuntime::getRuntime();
  37. LOGI("System server: starting Android services.\n" );
  38. runtime->callStatic("com/android/server/SystemServer" ,  "init2" );
  39. // If running in our own process, just go into the thread
  40. // pool.  Otherwise, call the initialization finished
  41. // func to let this process continue its initilization.
  42. if  (proc->supportsProcesses()) {
  43. LOGI("System server: entering thread pool.\n" );
  44. ProcessState::self()->startThreadPool();
  45. IPCThreadState::self()->joinThreadPool();
  46. LOGI("System server: exiting thread pool.\n" );
  47. }
  48. return  NO_ERROR;
  49. }

等初始化传感器,视频,音频等服务后,调用一个回调方法init2 (在SystemServer.java中)。在上面的代码可以看到,这个方法开启了ServerThread来初始化其它的服务。

Java代码  

  1. public   void  run() {
  2. EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,
  3. SystemClock.uptimeMillis());
  4. Looper.prepare();
  5. android.os.Process.setThreadPriority(
  6. android.os.Process.THREAD_PRIORITY_FOREGROUND);
  7. BinderInternal.disableBackgroundScheduling(true );
  8. android.os.Process.setCanSelfBackground(false );
  9. // Check whether we failed to shut down last time we tried.
  10. {
  11. final  String shutdownAction = SystemProperties.get(
  12. ShutdownThread.SHUTDOWN_ACTION_PROPERTY, "" );
  13. if  (shutdownAction !=  null  && shutdownAction.length() >  0 ) {
  14. boolean  reboot = (shutdownAction.charAt( 0 ) ==  ‘1‘ );
  15. final  String reason;
  16. if  (shutdownAction.length() >  1 ) {
  17. reason = shutdownAction.substring(1 , shutdownAction.length());
  18. } else  {
  19. reason = null ;
  20. }
  21. ShutdownThread.rebootOrShutdown(reboot, reason);
  22. }
  23. }
  24. String factoryTestStr = SystemProperties.get("ro.factorytest" );
  25. int  factoryTest =  "" .equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF
  26. : Integer.parseInt(factoryTestStr);
  27. LightsService lights = null ;
  28. PowerManagerService power = null ;
  29. BatteryService battery = null ;
  30. ConnectivityService connectivity = null ;
  31. IPackageManager pm = null ;
  32. Context context = null ;
  33. WindowManagerService wm = null ;
  34. BluetoothService bluetooth = null ;
  35. BluetoothA2dpService bluetoothA2dp = null ;
  36. HeadsetObserver headset = null ;
  37. DockObserver dock = null ;
  38. UsbService usb = null ;
  39. UiModeManagerService uiMode = null ;
  40. RecognitionManagerService recognition = null ;
  41. ThrottleService throttle = null ;
  42. // Critical services...
  43. try  {
  44. Slog.i(TAG, "Entropy Service" );
  45. ServiceManager.addService("entropy" ,  new  EntropyService());
  46. Slog.i(TAG, "Power Manager" );
  47. power = new  PowerManagerService();
  48. ServiceManager.addService(Context.POWER_SERVICE, power);
  49. Slog.i(TAG, "Activity Manager" );
  50. context = ActivityManagerService.main(factoryTest);
  51. Slog.i(TAG, "Telephony Registry" );
  52. ServiceManager.addService("telephony.registry" ,  new  TelephonyRegistry(context));
  53. AttributeCache.init(context);
  54. Slog.i(TAG, "Package Manager" );
  55. pm = PackageManagerService.main(context,
  56. factoryTest != SystemServer.FACTORY_TEST_OFF);
  57. ActivityManagerService.setSystemProcess();
  58. mContentResolver = context.getContentResolver();
  59. // The AccountManager must come before the ContentService
  60. try  {
  61. Slog.i(TAG, "Account Manager" );
  62. ServiceManager.addService(Context.ACCOUNT_SERVICE,
  63. new  AccountManagerService(context));
  64. } catch  (Throwable e) {
  65. Slog.e(TAG, "Failure starting Account Manager" , e);
  66. }
  67. Slog.i(TAG, "Content Manager" );
  68. ContentService.main(context,
  69. factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
  70. Slog.i(TAG, "System Content Providers" );
  71. ActivityManagerService.installSystemProviders();
  72. Slog.i(TAG, "Battery Service" );
  73. battery = new  BatteryService(context);
  74. ServiceManager.addService("battery" , battery);
  75. Slog.i(TAG, "Lights Service" );
  76. lights = new  LightsService(context);
  77. Slog.i(TAG, "Vibrator Service" );
  78. ServiceManager.addService("vibrator" ,  new  VibratorService(context));
  79. // only initialize the power service after we have started the
  80. // lights service, content providers and the battery service.
  81. power.init(context, lights, ActivityManagerService.getDefault(), battery);
  82. Slog.i(TAG, "Alarm Manager" );
  83. AlarmManagerService alarm = new  AlarmManagerService(context);
  84. ServiceManager.addService(Context.ALARM_SERVICE, alarm);
  85. Slog.i(TAG, "Init Watchdog" );
  86. Watchdog.getInstance().init(context, battery, power, alarm,
  87. ActivityManagerService.self());
  88. Slog.i(TAG, "Window Manager" );
  89. wm = WindowManagerService.main(context, power,
  90. factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
  91. ServiceManager.addService(Context.WINDOW_SERVICE, wm);
  92. ((ActivityManagerService)ServiceManager.getService("activity" ))
  93. .setWindowManager(wm);
  94. // Skip Bluetooth if we have an emulator kernel
  95. // TODO: Use a more reliable check to see if this product should
  96. // support Bluetooth - see bug 988521
  97. if  (SystemProperties.get( "ro.kernel.qemu" ).equals( "1" )) {
  98. Slog.i(TAG, "Registering null Bluetooth Service (emulator)" );
  99. ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null );
  100. } else   if  (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
  101. Slog.i(TAG, "Registering null Bluetooth Service (factory test)" );
  102. ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null );
  103. } else  {
  104. Slog.i(TAG, "Bluetooth Service" );
  105. bluetooth = new  BluetoothService(context);
  106. ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, bluetooth);
  107. bluetooth.initAfterRegistration();
  108. bluetoothA2dp = new  BluetoothA2dpService(context, bluetooth);
  109. ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
  110. bluetoothA2dp);
  111. int  bluetoothOn = Settings.Secure.getInt(mContentResolver,
  112. Settings.Secure.BLUETOOTH_ON, 0 );
  113. if  (bluetoothOn >  0 ) {
  114. bluetooth.enable();
  115. }
  116. }
  117. } catch  (RuntimeException e) {
  118. Slog.e("System" ,  "Failure starting core service" , e);
  119. }
  120. DevicePolicyManagerService devicePolicy = null ;
  121. StatusBarManagerService statusBar = null ;
  122. InputMethodManagerService imm = null ;
  123. AppWidgetService appWidget = null ;
  124. NotificationManagerService notification = null ;
  125. WallpaperManagerService wallpaper = null ;
  126. LocationManagerService location = null ;
  127. if  (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
  128. try  {
  129. Slog.i(TAG, "Device Policy" );
  130. devicePolicy = new  DevicePolicyManagerService(context);
  131. ServiceManager.addService(Context.DEVICE_POLICY_SERVICE, devicePolicy);
  132. } catch  (Throwable e) {
  133. Slog.e(TAG, "Failure starting DevicePolicyService" , e);
  134. }
  135. try  {
  136. Slog.i(TAG, "Status Bar" );
  137. statusBar = new  StatusBarManagerService(context);
  138. ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
  139. } catch  (Throwable e) {
  140. Slog.e(TAG, "Failure starting StatusBarManagerService" , e);
  141. }
  142. try  {
  143. Slog.i(TAG, "Clipboard Service" );
  144. ServiceManager.addService(Context.CLIPBOARD_SERVICE,
  145. new  ClipboardService(context));
  146. } catch  (Throwable e) {
  147. Slog.e(TAG, "Failure starting Clipboard Service" , e);
  148. }
  149. try  {
  150. Slog.i(TAG, "Input Method Service" );
  151. imm = new  InputMethodManagerService(context, statusBar);
  152. ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
  153. } catch  (Throwable e) {
  154. Slog.e(TAG, "Failure starting Input Manager Service" , e);
  155. }
  156. try  {
  157. Slog.i(TAG, "NetStat Service" );
  158. ServiceManager.addService("netstat" ,  new  NetStatService(context));
  159. } catch  (Throwable e) {
  160. Slog.e(TAG, "Failure starting NetStat Service" , e);
  161. }
  162. try  {
  163. Slog.i(TAG, "NetworkManagement Service" );
  164. ServiceManager.addService(
  165. Context.NETWORKMANAGEMENT_SERVICE,
  166. NetworkManagementService.create(context));
  167. } catch  (Throwable e) {
  168. Slog.e(TAG, "Failure starting NetworkManagement Service" , e);
  169. }
  170. try  {
  171. Slog.i(TAG, "Connectivity Service" );
  172. connectivity = ConnectivityService.getInstance(context);
  173. ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
  174. } catch  (Throwable e) {
  175. Slog.e(TAG, "Failure starting Connectivity Service" , e);
  176. }
  177. try  {
  178. Slog.i(TAG, "Throttle Service" );
  179. throttle = new  ThrottleService(context);
  180. ServiceManager.addService(
  181. Context.THROTTLE_SERVICE, throttle);
  182. } catch  (Throwable e) {
  183. Slog.e(TAG, "Failure starting ThrottleService" , e);
  184. }
  185. try  {
  186. Slog.i(TAG, "Accessibility Manager" );
  187. ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
  188. new  AccessibilityManagerService(context));
  189. } catch  (Throwable e) {
  190. Slog.e(TAG, "Failure starting Accessibility Manager" , e);
  191. }
  192. try  {
  193. /*
  194. * NotificationManagerService is dependant on MountService,
  195. * (for media / usb notifications) so we must start MountService first.
  196. */
  197. Slog.i(TAG, "Mount Service" );
  198. ServiceManager.addService("mount" ,  new  MountService(context));
  199. } catch  (Throwable e) {
  200. Slog.e(TAG, "Failure starting Mount Service" , e);
  201. }
  202. try  {
  203. Slog.i(TAG, "Notification Manager" );
  204. notification = new  NotificationManagerService(context, statusBar, lights);
  205. ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
  206. } catch  (Throwable e) {
  207. Slog.e(TAG, "Failure starting Notification Manager" , e);
  208. }
  209. try  {
  210. Slog.i(TAG, "Device Storage Monitor" );
  211. ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
  212. new  DeviceStorageMonitorService(context));
  213. } catch  (Throwable e) {
  214. Slog.e(TAG, "Failure starting DeviceStorageMonitor service" , e);
  215. }
  216. try  {
  217. Slog.i(TAG, "Location Manager" );
  218. location = new  LocationManagerService(context);
  219. ServiceManager.addService(Context.LOCATION_SERVICE, location);
  220. } catch  (Throwable e) {
  221. Slog.e(TAG, "Failure starting Location Manager" , e);
  222. }
  223. try  {
  224. Slog.i(TAG, "Search Service" );
  225. ServiceManager.addService(Context.SEARCH_SERVICE,
  226. new  SearchManagerService(context));
  227. } catch  (Throwable e) {
  228. Slog.e(TAG, "Failure starting Search Service" , e);
  229. }
  230. if  (INCLUDE_DEMO) {
  231. Slog.i(TAG, "Installing demo data..." );
  232. (new  DemoThread(context)).start();
  233. }
  234. try  {
  235. Slog.i(TAG, "DropBox Service" );
  236. ServiceManager.addService(Context.DROPBOX_SERVICE,
  237. new  DropBoxManagerService(context,  new  File( "/data/system/dropbox" )));
  238. } catch  (Throwable e) {
  239. Slog.e(TAG, "Failure starting DropBoxManagerService" , e);
  240. }
  241. try  {
  242. Slog.i(TAG, "Wallpaper Service" );
  243. wallpaper = new  WallpaperManagerService(context);
  244. ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
  245. } catch  (Throwable e) {
  246. Slog.e(TAG, "Failure starting Wallpaper Service" , e);
  247. }
  248. try  {
  249. Slog.i(TAG, "Audio Service" );
  250. ServiceManager.addService(Context.AUDIO_SERVICE, new  AudioService(context));
  251. } catch  (Throwable e) {
  252. Slog.e(TAG, "Failure starting Audio Service" , e);
  253. }
  254. try  {
  255. Slog.i(TAG, "Headset Observer" );
  256. // Listen for wired headset changes
  257. headset = new  HeadsetObserver(context);
  258. } catch  (Throwable e) {
  259. Slog.e(TAG, "Failure starting HeadsetObserver" , e);
  260. }
  261. try  {
  262. Slog.i(TAG, "Dock Observer" );
  263. // Listen for dock station changes
  264. dock = new  DockObserver(context, power);
  265. } catch  (Throwable e) {
  266. Slog.e(TAG, "Failure starting DockObserver" , e);
  267. }
  268. try  {
  269. Slog.i(TAG, "USB Service" );
  270. // Listen for USB changes
  271. usb = new  UsbService(context);
  272. ServiceManager.addService(Context.USB_SERVICE, usb);
  273. } catch  (Throwable e) {
  274. Slog.e(TAG, "Failure starting UsbService" , e);
  275. }
  276. try  {
  277. Slog.i(TAG, "UI Mode Manager Service" );
  278. // Listen for UI mode changes
  279. uiMode = new  UiModeManagerService(context);
  280. } catch  (Throwable e) {
  281. Slog.e(TAG, "Failure starting UiModeManagerService" , e);
  282. }
  283. try  {
  284. Slog.i(TAG, "Backup Service" );
  285. ServiceManager.addService(Context.BACKUP_SERVICE,
  286. new  BackupManagerService(context));
  287. } catch  (Throwable e) {
  288. Slog.e(TAG, "Failure starting Backup Service" , e);
  289. }
  290. try  {
  291. Slog.i(TAG, "AppWidget Service" );
  292. appWidget = new  AppWidgetService(context);
  293. ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
  294. } catch  (Throwable e) {
  295. Slog.e(TAG, "Failure starting AppWidget Service" , e);
  296. }
  297. try  {
  298. Slog.i(TAG, "Recognition Service" );
  299. recognition = new  RecognitionManagerService(context);
  300. } catch  (Throwable e) {
  301. Slog.e(TAG, "Failure starting Recognition Service" , e);
  302. }
  303. try  {
  304. Slog.i(TAG, "DiskStats Service" );
  305. ServiceManager.addService("diskstats" ,  new  DiskStatsService(context));
  306. } catch  (Throwable e) {
  307. Slog.e(TAG, "Failure starting DiskStats Service" , e);
  308. }
  309. }
  310. // make sure the ADB_ENABLED setting value matches the secure property value
  311. Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
  312. "1" .equals(SystemProperties.get( "persist.service.adb.enable" )) ?  1  :  0);
  313. // register observer to listen for settings changes
  314. mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
  315. false ,  new  AdbSettingsObserver());
  316. // Before things start rolling, be sure we have decided whether
  317. // we are in safe mode.
  318. final   boolean  safeMode = wm.detectSafeMode();
  319. if  (safeMode) {
  320. try  {
  321. ActivityManagerNative.getDefault().enterSafeMode();
  322. // Post the safe mode state in the Zygote class
  323. Zygote.systemInSafeMode = true ;
  324. // Disable the JIT for the system_server process
  325. VMRuntime.getRuntime().disableJitCompilation();
  326. } catch  (RemoteException e) {
  327. }
  328. } else  {
  329. // Enable the JIT for the system_server process
  330. VMRuntime.getRuntime().startJitCompilation();
  331. }
  332. // It is now time to start up the app processes...
  333. if  (devicePolicy !=  null ) {
  334. devicePolicy.systemReady();
  335. }
  336. if  (notification !=  null ) {
  337. notification.systemReady();
  338. }
  339. if  (statusBar !=  null ) {
  340. statusBar.systemReady();
  341. }
  342. wm.systemReady();
  343. power.systemReady();
  344. try  {
  345. pm.systemReady();
  346. } catch  (RemoteException e) {
  347. }
  348. // These are needed to propagate to the runnable below.
  349. final  StatusBarManagerService statusBarF = statusBar;
  350. final  BatteryService batteryF = battery;
  351. final  ConnectivityService connectivityF = connectivity;
  352. final  DockObserver dockF = dock;
  353. final  UsbService usbF = usb;
  354. final  ThrottleService throttleF = throttle;
  355. final  UiModeManagerService uiModeF = uiMode;
  356. final  AppWidgetService appWidgetF = appWidget;
  357. final  WallpaperManagerService wallpaperF = wallpaper;
  358. final  InputMethodManagerService immF = imm;
  359. final  RecognitionManagerService recognitionF = recognition;
  360. final  LocationManagerService locationF = location;
  361. // We now tell the activity manager it is okay to run third party
  362. // code.  It will call back into us once it has gotten to the state
  363. // where third party code can really run (but before it has actually
  364. // started launching the initial applications), for us to complete our
  365. // initialization.
  366. ((ActivityManagerService)ActivityManagerNative.getDefault())
  367. .systemReady(new  Runnable() {
  368. public   void  run() {
  369. Slog.i(TAG, "Making services ready" );
  370. if  (statusBarF !=  null ) statusBarF.systemReady2();
  371. if  (batteryF !=  null ) batteryF.systemReady();
  372. if  (connectivityF !=  null ) connectivityF.systemReady();
  373. if  (dockF !=  null ) dockF.systemReady();
  374. if  (usbF !=  null ) usbF.systemReady();
  375. if  (uiModeF !=  null ) uiModeF.systemReady();
  376. if  (recognitionF !=  null ) recognitionF.systemReady();
  377. Watchdog.getInstance().start();
  378. // It is now okay to let the various system services start their
  379. // third party code...
  380. if  (appWidgetF !=  null ) appWidgetF.systemReady(safeMode);
  381. if  (wallpaperF !=  null ) wallpaperF.systemReady();
  382. if  (immF !=  null ) immF.systemReady();
  383. if  (locationF !=  null ) locationF.systemReady();
  384. if  (throttleF !=  null ) throttleF.systemReady();
  385. }
  386. });
  387. // For debug builds, log event loop stalls to dropbox for analysis.
  388. if  (StrictMode.conditionallyEnableDebugLogging()) {
  389. Slog.i(TAG, "Enabled StrictMode for system server main thread." );
  390. }
  391. Looper.loop();
  392. Slog.d(TAG, "System ServerThread is exiting!" );
  393. }

这里启动的每一个进程都作为一个Dalvik线程而存在于SystemServer进程里面。

时间: 2024-08-02 09:06:30

Android systemserver分析ThrottleService 介绍的相关文章

Android性能分析工具介绍

1. Android系统性能调优工具介绍 http://blog.csdn.net/innost/article/details/9008691 TraceviewSystraceOprofile 2. [腾讯开源]Android性能测试工具APT使用指南 http://www.csdn.net/article/2014-04-23/2819366-tencent-APT-open-source-tool-guide APT源码地址:https://code.csdn.net/Tencent/a

Android多线程分析之三:Handler,Looper的实现

Android多线程分析之三:Handler,Looper的实现 罗朝辉 (http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 在前文<Android多线程分析之二:Thread的实现>中已经具体分析了Android Thread 是怎样创建,执行以及销毁的,其重点是对对应 native 方法进行分析,今天我将聚焦于 Android Framework 层多线程相关的类:Handler, Looper, MessageQueue, Message 以及它们与

SystemServer分析

1 SystemServer分析 SystemServer的进程名就是前面所说的"system_server",是zygote进程"app_process"fork出来的第一个子嗣,其重要性不言而喻.下面我们简称其为SS. 1.1  SS的诞生 先回顾一下SS是如何创建的: /*在zygoteinit.java的startsystemserver方法中*/ String args[] = { "--setuid=1000", "--se

Android多线程分析之四:MessageQueue的实现

罗朝辉 (http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 在前面两篇文章<Android多线程分析之二:Thread的实现>,<Android多线程分析之三:Handler,Looper的实现>中分别介绍了 Thread 的创建,运行,销毁的过程以及 Thread与 Handler,Looper 之间的关联:Thread 在其 run() 方法中创建和运行消息处理循环 Looper,而 Looper::loop() 方法不断地从 Messag

Android多线程分析之二:Thread的实现

Android多线程分析之二:Thread 罗朝辉 (http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 在前文<Android多线程分析之一:使用Thread异步下载图像>中演示了如何使用 Thread 处理异步事务.示例中这个 Java Thread 类都是位于 Framework 层的类,它自身是通过 JNI 转调 dalvik 里面的 Thread 相关方法实现的.因此要分析 Androd 中的线程,就需要分析这两层中的与线程相关的代码,这就是本文要

Android多线程分析之一:使用Thread异步下载图像

罗朝辉 (http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 打算整理一下对 Android Framework 中多线程相关知识的理解,主要集中在 Framework 层的 Thread, Handler, Looper, MessageQueue, Message, AysncTask,当然不可避免地要涉及到 native 方法,因此也会分析 dalvik 中和线程以及消息处理相关的代码:如 dalvik 中的 C++ Thread 类以及 Message

Android多线程分析之五:使用AsyncTask异步下载图像

Android多线程分析之五:使用AsyncTask异步下载图像 罗朝辉 (http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 在本系列文章的第一篇<Android多线程分析之中的一个:使用Thread异步下载图像>中.曾演示了怎样使用 Thread 来完毕异步任务. Android 为了简化在 UI 线程中完毕异步任务(毕竟 UI 线程是 app 最重要的线程).实现了一个名为 AysncTask 的模板类.使用 AysncTask 能够在异步任务进行的同

Android Binder分析二:Natvie Service的注冊

这一章我们通过MediaPlayerService的注冊来说明怎样在Native层通过binder向ServiceManager注冊一个service,以及client怎样通过binder向ServiceManager获得一个service,并调用这个Service的方法. Native Service的注冊 这里以MediaPlayerService举例来说明怎样在Native层注冊Service,首先来看main_mediaservice.cpp的main方法: int main(int a

Android Framework 分析---PackageManager 分析

在windowphone,ios和android中到目前为止,还是android的市场份额最大.个人认为除了google开源外,广大开发者早就了android的霸主地位.各位兄弟姐妹开发出各种各样的apk,才组成android的广阔天下.本篇主要分析一下android系统是针对处理这些apk的,主要涉及到pm这块的代码.分析这种底层服务,最好从android的开启启动流程中开始分析.因为这样才能更清楚的了解服务的启动流程. 1.在SystemServer.java 中启动PM android 开