{"id":1828,"date":"2023-03-28T22:51:40","date_gmt":"2023-03-28T14:51:40","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1828"},"modified":"2023-04-22T09:19:31","modified_gmt":"2023-04-22T01:19:31","slug":"non-intrusive-access-to-android-global-context","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/28\/non-intrusive-access-to-android-global-context\/","title":{"rendered":"Android\u5168\u5c40Context\u65e0\u4fb5\u5165\u5f0f\u83b7\u53d6"},"content":{"rendered":"<h2>\u80cc\u666f\u63cf\u8ff0<\/h2>\n<p>\u5f53\u6211\u4eec\u5728\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u6216\u8005\u81ea\u5df1\u5c01\u88c5\u5e93\uff0c\u5982\u679c\u9700\u8981\u9700\u8981\u7528\u5230Context\u65f6\uff0c\u4e00\u822c\u505a\u6cd5\u5c31\u662f\u5c06\u521d\u59cb\u5316\u65b9\u6cd5\u66b4\u9732\u7ed9\u8c03\u7528\u65b9\uff0c\u8ba9\u8c03\u7528\u65b9\u5728\u521d\u59cb\u5316\u7c7b\u5e93\u65f6\uff0c\u4f20\u5165Context\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">publi class App extends Application {\n    \/**\n     * \u662f\u5426\u662fDebug\u73af\u5883\n     *\/\n    public static final boolean IS_DEBUG = true;\n\n    private static App mContext;\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        \/\/ARouter\u521d\u59cb\u5316\n        if (IS_DEBUG) {\n            ARouter.openLog();\n            ARouter.openDebug();\n        }\n        ARouter.init(this);\n    }\n\n    public static Context getInstance() {\n        return mContext;\n    }\n}<\/code><\/pre>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u5176\u5b9e\u65e0\u4fb5\u5165\u5f0f\u83b7\u53d6Context\u7684\u5b9e\u73b0\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u4f7f\u7528\u4e00\u4e2a<code>ContentProvider<\/code>\uff0c<code>ContentProvider<\/code>\u7684<code>onCreate()<\/code>\u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u8c03\u7528<code>getContext()<\/code>\u5373\u53ef\u83b7\u53d6\u5230Context\uff0c\u518d\u9759\u6001\u53d8\u91cf\u4fdd\u5b58\uff0c\u540e\u7eed\u76f4\u63a5\u83b7\u53d6\u5373\u53ef\u3002<\/p>\n<h2>Picasso\u7684\u65e0\u4fb5\u5165\u5f0f\u83b7\u53d6Context<\/h2>\n<p>\u4e0a\u8ff0\u7684\u539f\u7406\uff0c\u5176\u5b9e\u662f\u4ecePicasso\u4e2d\u501f\u9274\u7684\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u5427\u3002<\/p>\n<p>Picasso\u5b9e\u4f8b\u83b7\u53d6\uff0c\u91cd\u70b9\u5728Double-Check\u5355\u4f8b\u4e2d\u7684<code>PicassoProvider.context<\/code>\uff0c\u8c03\u7528<code>PicassoProvider<\/code>\u7684context\u5c5e\u6027\u83b7\u53d6Context\u3002<\/p>\n<pre><code class=\"language-java\">static volatile Picasso singleton = null;\n\npublic static Picasso get() {\n  if (singleton == null) {\n    synchronized (Picasso.class) {\n      if (singleton == null) {\n        \/\/\u91cd\u70b9\u5728\u8fd9\u91cc\uff0cPicassoProvider.context\u83b7\u53d6Context\n        if (PicassoProvider.context == null) {\n          throw new IllegalStateException(&quot;context == null&quot;);\n        }\n        singleton = new Builder(PicassoProvider.context).build();\n      }\n    }\n  }\n  return singleton;\n}<\/code><\/pre>\n<p><code>PicassoProvider<\/code>\u5176\u5b9e\u662f\u4e00\u4e2a<code>ContentProvider<\/code>\u7684\u5b50\u7c7b\uff0c\u53ea\u8981\u5c06<code>PicassoProvider<\/code>\u5728<code>AndroidManifest.xml<\/code>\u6e05\u5355\u6587\u4ef6\u4e2d\u6ce8\u518c\uff0c\u542f\u52a8App\u65f6\uff0cAndroid\u6846\u67b6\u4f1a\u521d\u59cb\u5316\u8fd9\u4e2a<code>PicassoProvider<\/code>\uff0c<code>onCreate()<\/code>\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c<code>getContext()<\/code>\u4fdd\u5b58Context\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-java\">public final class PicassoProvider extends ContentProvider {\n  @SuppressLint(&quot;StaticFieldLeak&quot;) static Context context;\n\n  @Override public boolean onCreate() {\n    context = getContext();\n    return true;\n  }   \n}<\/code><\/pre>\n<p>check \u4e86\u4e00\u4e0b Picasso \u7684\u6700\u65b0\u4ee3\u7801\uff0c\u8fd1\u671f\u5df2\u7ecf\u5207\u6210 kotlin \u4e86\uff0c\u4e0d\u8fc7\u76f8\u4f3c\u7684\u65b9\u6848<\/p>\n<h2>\u4ee3\u7801\u5b9e\u73b0<\/h2>\n<p><code>ApplicationContextProvider<\/code>\uff0c\u548cPicasso\u4e00\u6837\uff0c\u662f<code>ContentProvider<\/code>\u7684\u5b50\u7c7b\u3002\u5728<code>onCreate()<\/code>\u4e2d\u83b7\u53d6\u5230Context\uff0c\u518d\u4fdd\u5b58\u5230<code>ContextProvider<\/code>\u5b9e\u4f8b\u4e2d\u3002<\/p>\n<pre><code class=\"language-java\">public class ApplicationContextProvider extends ContentProvider {\n    @SuppressLint(&quot;StaticFieldLeak&quot;)\n    static Context mContext;\n\n    @Override\n    public boolean onCreate() {\n        mContext = getContext();\n        return false;\n    }\n\n    \/\/...\u7701\u7565\u5176\u4ed6\u5fc5\u987b\u590d\u5199\u7684\u65b9\u6cd5\uff08\u7a7a\u5b9e\u73b0\u5373\u53ef\uff09\n}<\/code><\/pre>\n<p>\u5728<code>AndroidManifest.xml<\/code>\u4e2d\u6ce8\u518c<code>ApplicationContextProvider<\/code><\/p>\n<pre><code class=\"language-xml\">&lt;manifest xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n        package=&quot;com.app.contextprovider&quot;&gt;\n\n    &lt;application&gt;\n        &lt;!-- \u5168\u5c40Context\u63d0\u4f9b\u8005 --&gt;\n        &lt;provider\n            android:name=&quot;.ApplicationContextProvider&quot;\n            android:authorities=&quot;${applicationId}.contextprovider&quot;\n            android:exported=&quot;false&quot; \/&gt;\n    &lt;\/application&gt;\n&lt;\/manifest&gt;<\/code><\/pre>\n<p><code>ContextProvider<\/code>\uff0c\u63d0\u4f9b\u5168\u5c40Context\u7684\u5355\u4f8b\u7c7b\uff0c\u63d0\u4f9b<code>get()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5355\u4f8b\u5b9e\u4f8b\uff0c\u7b2c\u4e00\u6b21\u6784\u9020\u65f6\u624d\u4ece<code>ApplicationContextProvider<\/code>\u4e2d\u83b7\u53d6Context\u6765\u521d\u59cb\u5316\u81ea\u8eab\u3002<\/p>\n<pre><code class=\"language-java\">public class ContextProvider {\n    @SuppressLint(&quot;StaticFieldLeak&quot;)\n    private static volatile ContextProvider instance;\n    private Context mContext;\n\n    private ContextProvider(Context context) {\n        mContext = context;\n    }\n\n    \/**\n     * \u83b7\u53d6\u5b9e\u4f8b\n     *\/\n    public static ContextProvider get() {\n        if (instance == null) {\n            synchronized (ContextProvider.class) {\n                if (instance == null) {\n                    Context context = ApplicationContextProvider.mContext;\n                    if (context == null) {\n                        throw new IllegalStateException(&quot;context == null&quot;);\n                    }\n                    instance = new ContextProvider(context);\n                }\n            }\n        }\n        return instance;\n    }\n\n    \/**\n     * \u83b7\u53d6\u4e0a\u4e0b\u6587\n     *\/\n    public Context getContext() {\n        return mContext;\n    }\n\n    public Application getApplication() {\n        return (Application) mContext.getApplicationContext();\n    }\n}<\/code><\/pre>\n<p>\u4f7f\u7528\uff0c\u4f8b\u5982\u4f7f\u7528\u5168\u5c40Context\u53d1\u9001\u5e7f\u64ad<\/p>\n<pre><code class=\"language-java\">Intent intent = new Intent(&quot;action_update_user_info&quot;);\nContextProvider.get().getContext().sendBroadcast(intent);<\/code><\/pre>\n<h2>\u4e00\u4e9b\u5176\u4ed6\u5e93\u53c2\u8003<\/h2>\n<h3>leakcanary\u4e2d\u7684\u65e0\u4fb5\u5165\u5f0f\u521d\u59cb\u5316<\/h3>\n<p><code>leakcanary<\/code>\u662fAndroid\u975e\u5e38\u51fa\u540d\u7684\u5185\u5b58\u6cc4\u9732\u68c0\u6d4b\u5e93\uff0c\u7531square\u516c\u53f8\u51fa\u54c1\u3002\u4ee5\u524d\u7248\u672c\u7684\u9700\u8981\u6211\u4eec\u624b\u52a8\u8c03\u7528<code>install()<\/code>\u521d\u59cb\u5316\uff0c\u6700\u65b0\u7248\u5df2\u7ecf\u4e0d\u9700\u8981\u4e86\uff0c\u539f\u56e0\u5c31\u662f\u4e5f\u91c7\u7528<code>ContentProvider<\/code>\u81ea\u52a8\u88abAndroid\u6846\u67b6\u8c03\u7528\u7684\u65b9\u5f0f\uff0c\u83b7\u53d6\u5230Context\u540e\u8fdb\u884c<code>install()<\/code>\u3002<\/p>\n<p><code>AppWatcherInstaller<\/code>\uff0c\u4e5f\u662f<code>ContentProvider<\/code>\u7684\u5b50\u7c7b\u3002\u5728<code>onCreate()<\/code>\u65f6\uff0c\u8c03\u7528\u4e86<code>InternalAppWatcher<\/code>\u7684<code>install()<\/code>\u65b9\u6cd5\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<pre><code class=\"language-kotlin\">\/\/ContentProvider\u5b50\u7c7b\ninternal class AppWatcherInstaller : ContentProvider() {\n    override fun onCreate(): Boolean {\n        SharkLog.logger = DefaultCanaryLog()\n        val application = context!!.applicationContext as Application\n        \/\/\u65e0\u4fb5\u5165\u5f0f\u5b89\u88c5\n        InternalAppWatcher.install(application)\n        return true\n  }\n\n  \/\/\u7701\u7565\u5176\u4ed6\u4ee3\u7801...\n}\n\ninternal object InternalAppWatcher {\n  \/\/\u7701\u7565\u5176\u4ed6\u4ee3\u7801...\n\n  \/\/\u5b89\u88c5\n  fun install(application: Application) {\n    SharkLog.d { &quot;Installing AppWatcher&quot; }\n    checkMainThread()\n    \/\/\u5b89\u88c5\u8fc7\u5c31\u4e0d\u5b89\u88c5\u4e86\uff0c\u907f\u514d\u591a\u8fdb\u7a0b\u91cd\u590d\u521d\u59cb\u5316\n    if (this::application.isInitialized) {\n      return\n    }\n    InternalAppWatcher.application = application\n    \/\/\u5b89\u88c5\n    val configProvider = { AppWatcher.config }\n    ActivityDestroyWatcher.install(application, objectWatcher, configProvider)\n    FragmentDestroyWatcher.install(application, objectWatcher, configProvider)\n    onAppWatcherInstalled(application)\n  }\n\n  \/\/\u7701\u7565\u5176\u4ed6\u4ee3\u7801...\n}<\/code><\/pre>\n<h3>AutoSize\u5c4f\u5e55\u9002\u914d\u6846\u67b6\u81ea\u52a8\u521d\u59cb\u5316<\/h3>\n<p><code>AutoSize<\/code>\u662f\u4e00\u4e2a\u4eca\u65e5\u5934\u6761\u5c4f\u5e55\u9002\u914d\u65b9\u6848\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5185\u90e8\u4e5f\u6709\u4e00\u4e2a<code>InitProvider<\/code>\uff0c\u7ee7\u627f\u4e8e<code>ContentProvider<\/code>\uff0c\u5728<code>onCreate()<\/code>\u4e2d\u8c03\u7528<code>AutoSizeConfig<\/code>\u7c7b\u7684<code>init()<\/code>\u65b9\u6cd5\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u51cf\u5c11\u4e86\u914d\u7f6e\u3002<\/p>\n<pre><code class=\"language-java\">public class InitProvider extends ContentProvider {\n    @Override\n    public boolean onCreate() {\n        AutoSizeConfig.getInstance()\n                .setLog(true)\n                .init((Application) getContext().getApplicationContext())\n                .setUseDeviceSize(false);\n        return true;\n    }\n\n    \/\/\u7701\u7565\u5176\u4ed6\u4ee3\u7801...\n}<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p><code>ContentProvider<\/code>\u65b9\u6cd5\u65e0\u4fb5\u5165\u5f0f\u521d\u59cb\u5316\u65b9\u6848\u7684\u4f18\u7f3a\u70b9\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a\u5bf9\u4e8e\u56fa\u5b9a\u7684\u521d\u59cb\u5316\u914d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ContextProvider<\/code>\u65b9\u6848\u51cf\u5c11\u8c03\u7528\u65b9\u7684\u914d\u7f6e\uff0c\u51cf\u5c11\u51fa\u9519\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a\u5982\u679c\u521d\u59cb\u5316\u975e\u5e38\u8017\u65f6\uff0c\u65e0\u7591\u4f1a\u62d6\u6162App\u7684\u542f\u52a8\uff0c\u5982\u679c\u662f\u8017\u65f6\u521d\u59cb\u5316\uff0c\u5e94\u8be5\u63d0\u4f9b\u7ed9\u8c03\u7528\u65b9\u81ea\u884c\u51b3\u5b9a\uff0c\u4f8b\u5982\u5c06\u521d\u59cb\u5316\u63a8\u8fdf\u5230\u4e3b\u754c\u9762<code>onCreate()<\/code>\u65f6\u624d\u8c03\u7528\u521d\u59cb\u5316\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u672c\u6587\u8f6c\u8f7d\u81f3\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.jianshu.com\/p\/6f6d1352fb43\">https:\/\/www.jianshu.com\/p\/6f6d1352fb43<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f\u63cf\u8ff0 \u5f53\u6211\u4eec\u5728\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u6216\u8005\u81ea\u5df1\u5c01\u88c5\u5e93\uff0c\u5982\u679c\u9700\u8981\u9700\u8981\u7528\u5230Context\u65f6\uff0c\u4e00\u822c\u505a\u6cd5\u5c31\u662f\u5c06\u521d\u59cb\u5316\u65b9\u6cd5\u66b4\u9732 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[],"class_list":["post-1828","post","type-post","status-publish","format-standard","hentry","category-android-advance"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1828"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1828\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}