其实,说白了,学过JavaWeb的应该知道域,我感觉这个Context就跟域相似,存在于整个应用中
Context字面意思上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄,很多方法需要通过Context才能识别调用者的实例,比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的实例为Activity,而到了一个button的onClick(View view)等方法时,我们用this时就会报错,所以我们可能使用ActivityName.this来解决,主要原因是因为实现Context的类主要有Android特有的几个模型,Activity、Service以及BroadcastReceiver。
常规需要Context实例的方法主要有各种Service实现的类,比如说SensorManager在实例化时需要getSystemService(String)方法就必须由Context的实例执行,还有一些私有的文件系统I/O比如说openFileInput以及常用的Toast的makeText方法。
你的构造函数里面不是传入了一个上下文的对象吗?用它就行了啊。 Context是一个抽象对象,它的子类比较多,Activity就是它的子类,Service也是。
这样理解就很简单了:
一个APK进程只有一个Context: 这个Context就是ApplicationContext,从Context继承过来。
ApplicationContext可以看做是针对整个系统的全局处理接口,因为: 它负责和系统服务打交道 RPC通信由他通过那些XXXServiceManager和XXXService来处理。
其他一些模块,比如Activity,Service等,也是从Context继承而来的。 比如Acitivity在attach到主线程(ActivityThread)时,会用ApplicationContext来初始化这个Activity,这样就OK了。
比如Activity中StartService,调用过程如下: Activity.StartService (Activity继承自ContextWraper,实际会执行 ContextWraper中的) public ComponentName startService(Intent service) { return mBase.startService(service); }
这里面的mBase实际就是ApplicationContext
ApplicationContext中的实现如下: @Override public ComponentName startService(Intent service) { try { ComponentName cn = ActivityManagerNative.getDefault().startService( mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(getContentResolver())); if (cn != null && cn.getPackageName().equals("!")) { throw new SecurityException( "Not allowed to start service " + service + " without permission " + cn.getClassName()); } return cn; } catch (RemoteException e) { return null; } }
通过系统服务来做的。
总之:context就是将这些系统服务提供的功能,完美的包装起来了,其中的RPC过程,用户无需关心。好像这些功能就是在那,自己可以随便使用(要知道跨进程通信和调用,是非常难和麻烦的事情)。
|
分享到:
相关推荐
android context理解
2.非静态内部类(包括匿名内部类),它们会持有外部类的引用,导致外部类无法被释放,解决办法死是,使用静态内部类,如果传入了外部内引用,需要对该引用使用弱引用,并
spring-context-1.2.8.jar, spring-context-1.2.9.jar, spring-context-2.0-m2.jar, spring-context-2.0.1.jar, spring-context-2.0.2.jar, spring-context-2.0.4.jar, spring-context-2.0.6.jar, spring-context-...
主要介绍了使用Vue实现Context-Menu的思考与总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml...
家好,今天给大家分享一下Android里的Context的一些用法,以前经常有人在群里问我比如我在一个工具类里的某个方法,或者View里需要调用Context.但是工具类还有View里没有这个上下文怎么办?为了解决大家的疑问,我...
开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring...
赠送jar包:spring-context-5.2.15.RELEASE.jar; 赠送原API文档:spring-context-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-context-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-context-...
tive context-specific transition matrices. The adaptive context- specific input matrices capture external situations where user behaviors happen, such as time, location, weather and so on. And the ...
2、在Context:context.WithCancel(parentContext)创建 3、ctx,cancel := context.WithCancel(context.Background()) 4、当前Context被取消时,基于它的子context都会被取消 5、接受取消通知 <-ctx.Done() ...
赠送jar包:spring-context-support-1.0.10.jar; 赠送原API文档:spring-context-support-1.0.10-javadoc.jar; 赠送源代码:spring-context-support-1.0.10-sources.jar; 赠送Maven依赖信息文件:spring-context-...
v8_context_snapshot
爱上对方说道,Context jqueryContext jqueryContext jquery
Understanding Context
介绍一些基本的LaTeX与ConTeXt之间的对应关系,方便LaTeX用户转向ConTeXt
contextref.pdf context最新手册
赠送jar包:spring-context-5.3.7.jar; 赠送原API文档:spring-context-5.3.7-javadoc.jar; 赠送源代码:spring-context-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-context-5.3.7.pom; 包含翻译后的API...
赠送jar包:spring-cloud-context-3.1.1.jar; 赠送原API文档:spring-cloud-context-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-context-3.1.1-sources.jar; 赠送Maven依赖信息文件:spring-cloud-context-...
spring-context 源代码spring-context 源代码spring-context 源代码spring-context 源代码
spring-context-3.2.xsd