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);
获取系统/应用资源
// 如 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) { ... }
四大组件交互
// 如启动 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();
文件相关
// 如:获取缓存文件夹、删除文件、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);
数据库相关
// 如打开数据库、删除数据库、获取数据库路径等
public abstract SQLiteDatabase openOrCreateDatabase(...);
public abstract boolean deleteDatabase(String name);
public abstract File getDatabasePath(String name);
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