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。 Context提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型。同时启动应用级的操作,如启动Activity,broadcasting和接收intents。 下面介绍Context的一些get方法,通过这些get方法可以获取应用环境全局信息: Return the context of the single, global Application object of the current process. Return the full application info for this context's package. Return a ContentResolver instance for your application's package. Return PackageManager instance to find global package information. Return the name of this application's package. Return a Resources instance for your application's package. Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made. Return a localized string from the application's package's default string table. Return the handle to a system-level service by name. The class of the returned object varies by the requested name. Currently available names are: ? 还有很多有用的方法,具体不一一列举。详情请参考文档,反正Context很有用。1.public abstract Context?getApplicationContext?()
2.public abstract ApplicationInfo?getApplicationInfo?()
3.public abstract ContentResolver?getContentResolver?()
4.public abstract PackageManager?getPackageManager?()
5.public abstract String?getPackageName?()
6.public abstract Resources?getResources?()
7.public abstract SharedPreferences?getSharedPreferences?(String name, int mode)
8.public final String?getString?(int resId)
9.public abstract Object?getSystemService?(String name)
详细解决方案
Android中Context简介(1)
热度:36 发布时间:2016-05-01 19:24:01.0
相关解决方案
- Spring自动扫描 <context:componet-scan>和加了<context:include-filter>以后有什么区别
- Servlet.service() for servlet [jsp] in context with path解决方案
- @Context HttpServletRequest request,@Context HttpServletResponse response,该如何解决
- Context [] startup failed due to previous errors解决方案
- context.xml配置,连接mysql的有关问题
- ValueStack 和 Stack Context 有什么区别和联系?该怎么处理
- struts2开发时出现的异常 Exception sending context initialized event to listener instance
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网
- android 大量数据按周分组,该如何解决
- android RadioButton如何设置默认选中
- ksoap2-android-这个包,连接webService怎么设置超时
- android 怎么重新设置锚点
- android UI界面设计解决方案
- android 图片对象获取的有关问题
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderL,该如何处理
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderL解决思路
- android 怎么调用淘宝支付宝接口
- Android 沿袭InputMethodService自定义输入法
- android 关于服务连接的疑义
- android 两个activity如何通信
- android 怎么实现对view的放大和缩小
- android 教程解决方法
- ERROR [org.springframework.web.context.ContextLoader] Context initialization fai,该怎么处理
- android ID,该如何处理
- 准备复习2-3个月,看java+android,请问有经验者,怎么看效果最好》