android ContentProvider onCreate()在 Application onCreate()之前执行
16lz
2021-01-23
分析源码5.1.1 r1
源码地址 http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/app/ActivityThread.java#ActivityThread.installProvider%28android.content.Context%2Candroid.app.IActivityManager.ContentProviderHolder%2Candroid.content.pm.ProviderInfo%2Cboolean%2Cboolean%2Cboolean%29
主要看ActivityThread类
bind Application的方法
handleBindApplication(AppBindData data)
内部代码块
try {4524 // If the app is being launched for full backup or restore, bring it up in4525 // a restricted environment with the base application class.4526 Application app = data.info.makeApplication(data.restrictedBackupMode, null);4527 mInitialApplication = app;45284529 // don't bring up providers in restricted mode; they may depend on the4530 // app's custom Application class4531 if (!data.restrictedBackupMode) {4532 List providers = data.providers;4533 if (providers != null) {4534 installContentProviders(app, providers); //继续跟踪这个做了哪些事情4535 // For process that contains content providers, we want to4536 // ensure that the JIT is enabled "at some point".4537 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);4538 }4539 }45404541 // Do this after providers, since instrumentation tests generally start their4542 // test thread at this point, and we don't want that racing.4543 try {4544 mInstrumentation.onCreate(data.instrumentationArgs);4545 }4546 catch (Exception e) {4547 throw new RuntimeException(4548 "Exception thrown in onCreate() of "4549 + data.instrumentationName + ": " + e.toString(), e);4550 }45514552 try {4553 mInstrumentation.callApplicationOnCreate(app); // 执行Application.onCreate()4554 } catch (Exception e) {4555 if (!mInstrumentation.onException(app, e)) {4556 throw new RuntimeException(4557 "Unable to create application " + app.getClass().getName()4558 + ": " + e.toString(), e);4559 }4560 }
跟踪 installContentProviders(app, providers)
private void More ...installContentProviders(4581 Context context, List providers) {4582 final ArrayList results =4583 new ArrayList();45844585 for (ProviderInfo cpi : providers) {4586 if (DEBUG_PROVIDER) {4587 StringBuilder buf = new StringBuilder(128);4588 buf.append("Pub ");4589 buf.append(cpi.authority);4590 buf.append(": ");4591 buf.append(cpi.name);4592 Log.i(TAG, buf.toString());4593 }4594 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi, //继续跟踪这里4595 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);4596 if (cph != null) {4597 cph.noReleaseNeeded = true;4598 results.add(cph);4599 }4600 }46014602 try {4603 ActivityManagerNative.getDefault().publishContentProviders(4604 getApplicationThread(), results);4605 } catch (RemoteException ex) {4606 }4607 }
跟踪 installProvider(context, null, cpi,...
try {4986 final java.lang.ClassLoader cl = c.getClassLoader();4987 localProvider = (ContentProvider)cl.4988 loadClass(info.name).newInstance();4989 provider = localProvider.getIContentProvider();4990 if (provider == null) {4991 Slog.e(TAG, "Failed to instantiate class " +4992 info.name + " from sourceDir " +4993 info.applicationInfo.sourceDir);4994 return null;4995 }4996 if (DEBUG_PROVIDER) Slog.v(4997 TAG, "Instantiating local provider " + info.name);4998 // XXX Need to create the correct context for this provider.4999 localProvider.attachInfo(c, info); //继续跟踪这里5000 } catch (java.lang.Exception e) {5001 if (!mInstrumentation.onException(null, e)) {5002 throw new RuntimeException(5003 "Unable to get provider " + info.name5004 + ": " + e.toString(), e);5005 }5006 return null;5007 }
跟踪 localProvider.attachInfo(c, info)
1674 private void More ...attachInfo(Context context, ProviderInfo info, boolean testing) {1675 mNoPerms = testing;16761677 /*1678 * Only allow it to be set once, so after the content service gives1679 * this to us clients can't change it.1680 */1681 if (mContext == null) {1682 mContext = context;1683 if (context != null) {1684 mTransport.mAppOpsManager = (AppOpsManager) context.getSystemService(1685 Context.APP_OPS_SERVICE);1686 }1687 mMyUid = Process.myUid();1688 if (info != null) {1689 setReadPermission(info.readPermission);1690 setWritePermission(info.writePermission);1691 setPathPermissions(info.pathPermissions);1692 mExported = info.exported;1693 mSingleUser = (info.flags & ProviderInfo.FLAG_SINGLE_USER) != 0;1694 setAuthorities(info.authority);1695 }1696 ContentProvider.this.onCreate(); // 调用ContentProvider onCreate()1697 }1698 }
通过查看源码的方式验证ContentProvider onCreate()优先执行与 Application onCreate()
更多相关文章
- android所有版本源码下载网站
- Android 4.0系统源码目录结构详解
- 仿照利用android系统源码资源文件,修改SeekBar颜色 前景与背景
- zxing项目源码解读(2.3.0版本,Android部分)
- Android事件分发机制源码分析
- vim+ctags+cscope 打造Android源码阅读工具
- Android 底层渲染 - 屏幕刷新机制源码分析
- Android源码分析(四)-----Android源码编译及刷机步骤
- android中Handler的源码分析