Context上下文对象
通过它可以获取应用整体信息的接口:
直接实现类
ContextWrapper
MockContext
间接实现类:
ContextThemeWrapper acticity就是集成这个类 所有activity里面获取相关资源的api全部从这个地方来的
常用api
Context类简单介绍
1:提供创建文件的模式 eg:public static final int MODE_PRIVATE = 0x0000;
2:提供bindService的相关模式 public static final int BIND_AUTO_CREATE = 0x0001;
3:获取sharepreferense存储数据 getSharedPreferences
4:数据库操作api
5:getAssets()
6:getResources();
7:getPackageManager()
8:getContentResolver()
9:getMainLooper()
10:getApplicationContext()
11: public final CharSequence getText(@StringRes int resId) {
return getResources().getText(resId);
}
12:public final int getColor(int id) {
return getResources().getColor(id, getTheme());
}
13:public final Drawable getDrawable(int id) {
return getResources().getDrawable(id, getTheme());
}
14:public final ColorStateList getColorStateList(int id) {
return getResources().getColorStateList(id, getTheme());
}
15:public abstract void setTheme(@StyleRes int resid);
16:public final TypedArray obtainStyledAttributes(@StyleableRes int[] attrs) {
return getTheme().obtainStyledAttributes(attrs);
}
17:public final TypedArray obtainStyledAttributes(
@StyleRes int resid, @StyleableRes int[] attrs) throws Resources.NotFoundException {
return getTheme().obtainStyledAttributes(resid, attrs);
}
18:public final TypedArray obtainStyledAttributes(
AttributeSet set, @StyleableRes int[] attrs) {
return getTheme().obtainStyledAttributes(set, attrs, 0, 0);
}
19:public abstract ClassLoader getClassLoader();
20:public abstract String getPackageName();
public abstract String getBasePackageName();
21:public abstract ApplicationInfo getApplicationInfo();
22:public abstract String getPackageResourcePath();
23:public abstract String getPackageCodePath();
24:public abstract File getSharedPrefsFile(String name);
25:public abstract SharedPreferences getSharedPreferences(String name,
int mode);
26:/**
* Open a private file associated with this Context's application package
* for reading.
*
* @param name The name of the file to open; can not contain path
* separators.
*
* @return The resulting {@link FileInputStream}.
*
* @see #openFileOutput
* @see #fileList
* @see #deleteFile
* @see java.io.FileInputStream#FileInputStream(String)
*/
public abstract FileInputStream openFileInput(String name)
throws FileNotFoundException;
27:/**
* Open a private file associated with this Context's application package
* for writing. Creates the file if it doesn't already exist.
*
*
No permissions are required to invoke this method, since it uses internal
* storage.
*
* @param name The name of the file to open; can not contain path
* separators.
* @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the
* default operation, {@link #MODE_APPEND} to append to an existing file,
* {@link #MODE_WORLD_READABLE} and {@link #MODE_WORLD_WRITEABLE} to control
* permissions.
*
* @return The resulting {@link FileOutputStream}.
*
* @see #MODE_APPEND
* @see #MODE_PRIVATE
* @see #MODE_WORLD_READABLE
* @see #MODE_WORLD_WRITEABLE
* @see #openFileInput
* @see #fileList
* @see #deleteFile
* @see java.io.FileOutputStream#FileOutputStream(String)
*/
public abstract FileOutputStream openFileOutput(String name, int mode)
throws FileNotFoundException;
28:/**
* Delete the given private file associated with this Context's
* application package.
*
* @param name The name of the file to delete; can not contain path
* separators.
*
* @return {@code true} if the file was successfully deleted; else
* {@code false}.
*
* @see #openFileInput
* @see #openFileOutput
* @see #fileList
* @see java.io.File#delete()
*/
public abstract boolean deleteFile(String name);
29:public abstract File getFileStreamPath(String name);
30:/**
* Returns the absolute path to the directory on the filesystem where
* files created with {@link #openFileOutput} are stored.
*
*
No permissions are required to read or write to the returned path, since this
* path is internal storage.
*
* @return The path of the directory holding application files.
*
* @see #openFileOutput
* @see #getFileStreamPath
* @see #getDir
*/
public abstract File getFilesDir();
31:/**
* Returns the absolute path to the directory on the primary external filesystem
* (that is somewhere on {@link android.os.Environment#getExternalStorageDirectory()
* Environment.getExternalStorageDirectory()}) where the application can
* place persistent files it owns. These files are internal to the
* applications, and not typically visible to the user as media.
*
*
This is like {@link #getFilesDir()} in that these
* files will be deleted when the application is uninstalled, however there
* are some important differences:
*
*
Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, no permissions
* are required to read or write to the returned path; it's always
* accessible to the calling app. This only applies to paths generated for
* package name of the calling application. To access paths belonging
* to other packages, {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE}
* and/or {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} are required.
*
*
On devices with multiple users (as described by {@link UserManager}),
* each user has their own isolated external storage. Applications only
* have access to the external storage for the user they're running as.
Here is an example of typical code to manipulate a file in
* an application's private storage:
If you supply a non-null type to this function, the returned
* file will be a path to a sub-directory of the given type. Though these files
* are not automatically scanned by the media scanner, you can explicitly
* add them to the media database with
* {@link android.media.MediaScannerConnection#scanFile(Context, String[], String[],
* android.media.MediaScannerConnection.OnScanCompletedListener)
* MediaScannerConnection.scanFile}.
* Note that this is not the same as
* {@link android.os.Environment#getExternalStoragePublicDirectory
* Environment.getExternalStoragePublicDirectory()}, which provides
* directories of media shared by all applications. The
* directories returned here are
* owned by the application, and their contents will be removed when the
* application is uninstalled. Unlike
* {@link android.os.Environment#getExternalStoragePublicDirectory
* Environment.getExternalStoragePublicDirectory()}, the directory
* returned here will be automatically created for you.
*
*
Here is an example of typical code to manipulate a picture in
* an application's private storage and add it to the media database:
51:public abstract int getUserId();