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方法可以获取应用环境全局信息:
1.public
abstract
Context
getApplicationContext
()
Return the context of the
single, global Application object of the
current process.
2.public
abstract
ApplicationInfo
getApplicationInfo
()
Return the full application
info for this context's package.
3.public
abstract
ContentResolver
getContentResolver
()
Return a ContentResolver
instance for your application's package.
4.public
abstract
PackageManager
getPackageManager
()
Return PackageManager instance
to find global package information.
5.public
abstract
String
getPackageName
()
Return the name of this
application's package.
6.public
abstract
Resources
getResources
()
Return a Resources instance for
your application's package.
7.public
abstract
SharedPreferences
getSharedPreferences
(String
name, int mode)
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.
8.public
final
String
getString
(int resId)
Return a localized string from
the application's package's
default string table.
9.public
abstract
Object
getSystemService
(String
name)
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很有用。
分享到:
相关推荐
大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml...
Android Context使用例子.
android:name="工具类的路径" 调用: Toast.makeText(MainApplication.getContext(), "文本", Toast.LENGTH_SHORT).show(); 不管你想在项目的任何地方使用Context,只需要调用一下MainApplication.getContext()...
简要介绍android context 的用法
家好,今天给大家分享一下Android里的Context的一些用法,以前经常有人在群里问我比如我在一个工具类里的某个方法,或者View里需要调用Context.但是工具类还有View里没有这个上下文怎么办?为了解决大家的疑问,我...
Android全局变量和Context的实现方法
android_context详解,深入剖析
012_android 之消息提示toast 和Context视频教材,讲解的比较详细,有兴趣的可以学习下哦。
本文主要介绍Android中Context引起的内存泄露的问题,这里对Context的知识做了详细讲解,说明如何避免内存泄漏的问题,有兴趣的小伙伴可以参考下
主要介绍了Android 中Context的使用方法详解的相关资料,希望通过本文大家能够理解掌握context的使用方法,需要的朋友可以参考下
以下分别通过Context认知角度,继承关系,对象创建等方面android中Context做了深入的解释,一起学习下。 1、Context认知。 Context译为场景,一个应用程序可以认为是一个工作环境,在这个工作环境中可以存在许多场景...
本文实例讲述了Android编程中context及全局变量的用法。分享给大家供大家参考,具体如下: 今天在研究context的时候,对application和activity context有了一定的了解,下面是从网上复制过来的资料 Application ...
android context理解
android Context Menu With Icon
Android-Context-Menu.Android.zip,你可以很容易地添加令人敬畏的动画上下文菜单到你的应用程序。,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有...
android demo,BaseAdapter传递context的应用,
Context-Menu.Android源码,是一个很不错的C++代码,有兴趣的伙伴们抽时间可以看一下把。
主要介绍了Android编程获取全局Context的方法,实例分析了基于Application类获取全局Context的实现步骤与相关技巧,需要的朋友可以参考下
主要介绍了Android中的Context抽象类,包括Context的实例化与获取App的Context等方法,需要的朋友可以参考下