android Context上下文详细了解以及使用

[color=red][size=medium][b]1.Context基本概念[/b][/size][/color]

[b][color=blue]Context是什么[/color][/b]

[b][color=red]1) Context是一个抽象类,其通用实现在ContextImpl类中。[/color][/b]

[color=blue][b]2) Context:是一个访问Application环境全局信息的接口,通过它可以访问application的资源和相关的类[/b][/color],其主要功能如下:

A.[b]启动Activity[/b]
B.[b]启动和停止Service[/b]
C.[b]发送广播消息(Intent)[/b]
D.[b]注册广播消息(Intent)接收者[/b]
[color=red]可以访问APK中各种资源(如Resources和AssetManager等)[/color]
[color=red]可以访问Package的相关信息[/color]
[b][color=blue]APK的各种权限管理[/color][/b]

从以上分析可以看出,[color=red][b]Context就是一个对APK包无所不知的大管家[/b][/color],大家需要什么,直接问它就可以了。

[color=red][size=medium][b]2.Context与View的关系[/b][/size][/color]
[b]View与Context(或Activity)的关系类似于明星与经纪人的关系,所以创建View时,必须明确指定其Context(即经纪人或大管家),否则View就成不了明星。[/b]

[color=blue][b]Context家族关系:[/b][/color]

[img]http://dl2.iteye.com/upload/attachment/0114/0160/140ea7e0-3d9f-319f-b4f9-5e84b60d4b8b.jpg[/img]

[b]Context类内容[/b]

public abstract class Context {

// 获取应用程序包的AssetManager实例
public abstract AssetManager getAssets();

// 获取应用程序包的Resources实例
public abstract Resources getResources();

// 获取PackageManager实例,以查看全局package信息
public abstract PackageManager getPackageManager();

// 获取应用程序包的ContentResolver实例
public abstract ContentResolver getContentResolver();

// 它返回当前进程的主线程的Looper,此线程分发调用给应用组件(activities, services等)
public abstract Looper getMainLooper();

// 返回当前进程的单实例全局Application对象的Context
public abstract Context getApplicationContext();

// 从string表中获取本地化的、格式化的字符序列
public final CharSequence getText(int resId) {
return getResources().getText(resId);
}

// 从string表中获取本地化的字符串
public final String getString(int resId) {
return getResources().getString(resId);
}

public final String getString(int resId, Object... formatArgs) {
return getResources().getString(resId, formatArgs);
}

// 返回一个可用于获取包中类信息的class loader
public abstract ClassLoader getClassLoader();

// 返回应用程序包名
public abstract String getPackageName();

// 返回应用程序信息
public abstract ApplicationInfo getApplicationInfo();

// 根据文件名获取SharedPreferences
public abstract SharedPreferences getSharedPreferences(String name, int mode);

// 其根目录为: Environment.getExternalStorageDirectory()
/*
* @param type The type of files directory to return. May be null for the
* root of the files directory or one of the following Environment constants
* for a subdirectory: {@link android.os.Environment#DIRECTORY_MUSIC},
* {@link android.os.Environment#DIRECTORY_PODCASTS}, {@link
* android.os.Environment#DIRECTORY_RINGTONES}, {@link
* android.os.Environment#DIRECTORY_ALARMS}, {@link
* android.os.Environment#DIRECTORY_NOTIFICATIONS}, {@link
* android.os.Environment#DIRECTORY_PICTURES}, or {@link
* android.os.Environment#DIRECTORY_MOVIES}.
*/
public abstract File getExternalFilesDir(String type);

// 返回应用程序obb文件路径
public abstract File getObbDir();

// 启动一个新的activity
public abstract void startActivity(Intent intent);

// 启动一个新的activity
public void startActivityAsUser(Intent intent, UserHandle user) {
throw new RuntimeException(
"Not implemented. Must override in a subclass.");
}

// 启动一个新的activity
// intent: 将被启动的activity的描述信息
// options: 描述activity将如何被启动
public abstract void startActivity(Intent intent, Bundle options);

// 启动多个新的activity
public abstract void startActivities(Intent[] intents);

// 启动多个新的activity
public abstract void startActivities(Intent[] intents, Bundle options);

// 广播一个intent给所有感兴趣的接收者,异步机制
public abstract void sendBroadcast(Intent intent);

// 广播一个intent给所有感兴趣的接收者,异步机制
public abstract void sendBroadcast(Intent intent, String receiverPermission);

public abstract void sendOrderedBroadcast(Intent intent,
String receiverPermission);

public abstract void sendOrderedBroadcast(Intent intent,
String receiverPermission, BroadcastReceiver resultReceiver,
Handler scheduler, int initialCode, String initialData,
Bundle initialExtras);

public abstract void sendBroadcastAsUser(Intent intent, UserHandle user);

public abstract void sendBroadcastAsUser(Intent intent, UserHandle user,
String receiverPermission);

// 注册一个BroadcastReceiver,且它将在主activity线程中运行
public abstract Intent registerReceiver(BroadcastReceiver receiver,
IntentFilter filter);

public abstract Intent registerReceiver(BroadcastReceiver receiver,
IntentFilter filter, String broadcastPermission, Handler scheduler);

public abstract void unregisterReceiver(BroadcastReceiver receiver);

// 请求启动一个application service
public abstract ComponentName startService(Intent service);

// 请求停止一个application service
public abstract boolean stopService(Intent service);

// 连接一个应用服务,它定义了application和service间的依赖关系
public abstract boolean bindService(Intent service, ServiceConnection conn,
int flags);

// 断开一个应用服务,当服务重新开始时,将不再接收到调用,
// 且服务允许随时停止
public abstract void unbindService(ServiceConnection conn);

// 返回系统级service句柄
/*
* @see #WINDOW_SERVICE
*
* @see android.view.WindowManager
*
* @see #LAYOUT_INFLATER_SERVICE
*
* @see android.view.LayoutInflater
*
* @see #ACTIVITY_SERVICE
*
* @see android.app.ActivityManager
*
* @see #POWER_SERVICE
*
* @see android.os.PowerManager
*
* @see #ALARM_SERVICE
*
* @see android.app.AlarmManager
*
* @see #NOTIFICATION_SERVICE
*
* @see android.app.NotificationManager
*
* @see #KEYGUARD_SERVICE
*
* @see android.app.KeyguardManager
*
* @see #LOCATION_SERVICE
*
* @see android.location.LocationManager
*
* @see #SEARCH_SERVICE
*
* @see android.app.SearchManager
*
* @see #SENSOR_SERVICE
*
* @see android.hardware.SensorManager
*
* @see #STORAGE_SERVICE
*
* @see android.os.storage.StorageManager
*
* @see #VIBRATOR_SERVICE
*
* @see android.os.Vibrator
*
* @see #CONNECTIVITY_SERVICE
*
* @see android.net.ConnectivityManager
*
* @see #WIFI_SERVICE
*
* @see android.net.wifi.WifiManager
*
* @see #AUDIO_SERVICE
*
* @see android.media.AudioManager
*
* @see #MEDIA_ROUTER_SERVICE
*
* @see android.media.MediaRouter
*
* @see #TELEPHONY_SERVICE
*
* @see android.telephony.TelephonyManager
*
* @see #INPUT_METHOD_SERVICE
*
* @see android.view.inputmethod.InputMethodManager
*
* @see #UI_MODE_SERVICE
*
* @see android.app.UiModeManager
*
* @see #DOWNLOAD_SERVICE
*
* @see android.app.DownloadManager
*/
public abstract Object getSystemService(String name);

public abstract int checkPermission(String permission, int pid, int uid);

// 返回一个新的与application name对应的Context对象
public abstract Context createPackageContext(String packageName, int flags)
throws PackageManager.NameNotFoundException;

// 返回基于当前Context对象的新对象,其资源与display相匹配
public abstract Context createDisplayContext(Display display);
}


[color=red][size=medium][b]3.何时创建Context[/b][/size][/color]

[color=blue][b]应用程序在以下几种情况下创建Context实例:[/b][/color]

[color=red]1) 创建Application对象时,而且整个App共一个Application对象[/color]

[color=red]2) 创建Service对象时[/color]

[color=red]3) 创建Activity对象时[/color]

因此应用程序App共有的Context数目公式为:

[size=medium][color=red]总Context实例个数 = Service个数 + Activity个数 + 1(Application对应的Context实例)[/color][/size]

通过对ContextImp的分析可知,其方法的大多数操作都是直接调用其属性mPackageInfo(该属性类型为PackageInfo)的相关方法而来。[color=red]这说明ContextImp是一种轻量级类,而PackageInfo才是真正重量级的类。[/color][b]而一个App里的所有ContextImpl实例,都对应同一个packageInfo对象。[/b]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值