• 安卓基础知识:Context解析


    作用

    Activity本身继承了Context抽象类,可以通过Context获取一些相关信息
    获取应用相关信息

     // 作用1:获取应用相关信息
        public abstract ApplicationInfo getApplicationInfo();
        public abstract String getPackageName();
        public abstract Looper getMainLooper();
        public abstract int checkPermission(@NonNull String permission, int pid, int uid);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    获取系统/应用资源

    // 如 AssetManager、PackageManager、Resources、System Service 以及 color、string、drawable 等
        public abstract AssetManager getAssets();
        public abstract Resources getResources();
        public abstract PackageManager getPackageManager();
        public abstract Context getApplicationContext();
        public abstract ClassLoader getClassLoader();
        public final @Nullable <T> T getSystemService(@NonNull Class<T> serviceClass) { ... }
        public final String getString(@StringRes int resId) { ... }
        public final int getColor(@ColorRes int id) { ... }
        public final Drawable getDrawable(@DrawableRes int id) { ... }
        public abstract Resources.Theme getTheme();
        public abstract void setTheme(@StyleRes int resid);
        public final TypedArray obtainStyledAttributes(@StyleableRes int[] attrs) { ... }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    四大组件交互

    // 如启动 Activity、Broadcast、Service,获取 ContentResolver 等
        public abstract void startActivity(@RequiresPermission Intent intent);
        public abstract void sendBroadcast(@RequiresPermission Intent intent);
        public abstract Intent registerReceiver(@Nullable BroadcastReceiver receiver,
                                                IntentFilter filter);
        public abstract void unregisterReceiver(BroadcastReceiver receiver);
        public abstract ComponentName startService(Intent service);
        public abstract boolean stopService(Intent service);
        public abstract boolean bindService(@RequiresPermission Intent service,
                @NonNull ServiceConnection conn, @BindServiceFlags int flags);
        public abstract void unbindService(@NonNull ServiceConnection conn);
        public abstract ContentResolver getContentResolver();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    文件相关

    // 如:获取缓存文件夹、删除文件、SharedPreference 相关等
        public abstract File getSharedPreferencesPath(String name);
        public abstract File getDataDir();
        public abstract boolean deleteFile(String name);
        public abstract File getExternalFilesDir(@Nullable String type);
        public abstract File getCacheDir();
        ...
        public abstract SharedPreferences getSharedPreferences(String name, @PreferencesMode int mode);
        public abstract boolean deleteSharedPreferences(String name);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    数据库相关

    // 如打开数据库、删除数据库、获取数据库路径等
        public abstract SQLiteDatabase openOrCreateDatabase(...);
        public abstract boolean deleteDatabase(String name);
        public abstract File getDatabasePath(String name);
    
    • 1
    • 2
    • 3
    • 4

    不同类型

    ActivityContext:与UI相关,对UI资源进行操作
    ApplicationContext:生命周期较长(ApplicaitonContext生命周期与应用保持一致,注意内存泄漏)

    额外问题

    Activity继承ContextThemeWrapper来间接继承ContextWrapper,而Service与Applicaiton直接继承ContextWrapper,为什么需要间接继承?
    因为只有Activity需要在ContextThemeWrapper内包含的Theme接口

    getApplication()与getApplicationContext()有什么区别?
    返回的都是同一个Applicaiton对象,但作用域不同:
    getApplication():作用在Activity、Service,不能用在BroadcastReceiver
    getApplicationContext():作用域更广,可在BroadcastReceiver中使用

    getApplicationContext()、getBaseContext()和Activity.this区别?
    getApplicationContext():返回Applicaiton类型的Context
    getBaseContext():返回在构造函数/setBaseContext()里执行的Context
    Activity.this:返回当前Activity的Context

  • 相关阅读:
    Python中套接字实现服务端和客户端3-3
    06.2_c/c++开源库boost_coroutine2 协程库
    基于体素的三维表面重建: DLGAN, 3D GAN,3D-R2N2等
    蓝桥杯单片机小板100*100简化电路板
    基于AI与物联网技术的智能视频监控系统架构剖析
    后端的datetime类型数据转为string作为(vue)前端的时间YYYY-MM-DD HH:mm:ss
    【汇编语言】3.汇编语言程序
    MongoDB聚集分析
    一篇短小精悍的文章让你彻底明白KMP算法中next数组的原理
    Bridge Champ与Ignis公链:探索Web3游戏的新未来
  • 原文地址:https://blog.csdn.net/MagicSound/article/details/125635858