- 浏览: 634516 次
文章分类
最新评论
-
树下白狐:
listview与adapter用法 -
u011467537:
...
Android背景渐变色(shape,gradient) -
asdf12343800:
asdf12343800 写道asdf12343800 写道a ...
Android背景渐变色(shape,gradient) -
asdf12343800:
asdf12343800 写道asdf12343800 写道 ...
Android背景渐变色(shape,gradient) -
asdf12343800:
asdf12343800 写道 : lol: fghhg
Android背景渐变色(shape,gradient)
Android中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提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型。同时启动应用级的操作,如启动Activity,broadcasting和接收intents。
protected void onCreate(Bundle state) { //传递context给view control |
public class myactivity extends Activity { |
- 不要让生命周期长的对象引用activity context,即保证引用activity的对象要与activity本身生命周期是一样的
- 对于生命周期长的对象,可以使用application context
- 避免非静态的内部类,尽量使用静态类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化
|
下面介绍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.
发表评论
-
android layout查找顺序
2013-01-07 14:11 1148layout可以按后缀支持多分辨率,分辨率layout-mdp ... -
listview与adapter用法
2012-09-11 08:59 97438一个ListView通常有两 ... -
intent 传值
2012-09-11 08:58 1087intent.putExtra()存 getInten ... -
android(java)控制http连接超时的问题
2012-09-10 15:39 1532"声明一个boolean公共变量,表明当前httpc ... -
android 设置http等待超时
2012-09-10 15:39 11613BasicHttpParams httpPar ... -
android 设置椭圆
2012-09-10 15:39 2342<shape xmlns:android=& ... -
Android 自定义Button按钮显示样式(正常、按下、获取焦点)
2012-09-10 15:39 1297首先写一个定义Button样式的XML文件:新建Andr ... -
如何检查Android网络连接状态
2012-09-10 15:40 8703在发送任何HTTP请求前最 ... -
android 按两次返回键退出
2012-09-07 11:12 5383package com.anjoyo.test; ... -
Android Animation(二)
2012-09-07 10:58 951关于动画的实现,Andr ... -
activity切换动画
2012-09-07 10:55 4188在介绍切换动画效果前,先介绍下将使用到的Android ... -
android 震动
2012-09-07 10:02 2065让手机震动,需要创建Vibrator对象,通过调用vi ... -
android.R.layout下有一些常量存在哪?
2012-09-07 00:35 1162在你的android SDK目录下可以看到 例如 你的 and ... -
Android禁止横屏竖屏切换
2012-09-05 13:43 994在Android中要让一个程序的界面始终保持一个方向,不 ... -
ListView滚动加载(类似Android Market的列表)
2012-09-04 21:08 1764ListView滚动加载(类似Android Mark ... -
android 设置EditText只能输入数字
2012-09-03 23:23 47696android:numeric="intege ... -
填充Listview第一个Item的分隔线
2012-08-31 15:53 63181. 在res/drawable/目录下新建 mygra ... -
android相对父控件比例
2012-08-31 15:39 971shape xml中的gradient能用 70%p 这 ... -
分割线样式
2012-08-31 14:09 1091android给listview设置分割线Divider ... -
Android时钟
2012-08-31 13:21 21361. Android时钟,效果图: 2. ...
相关推荐
以下分别通过Context认知角度,继承关系,对象创建等方面android中Context做了深入的解释,一起学习下。 1、Context认知。 Context译为场景,一个应用程序可以认为是一个工作环境,在这个工作环境中可以存在许多场景...
本文主要介绍Android中Context引起的内存泄露的问题,这里对Context的知识做了详细讲解,说明如何避免内存泄漏的问题,有兴趣的小伙伴可以参考下
主要介绍了Android 中Context的使用方法详解的相关资料,希望通过本文大家能够理解掌握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的时候,对application和activity context有了一定的了解,下面是从网上复制过来的资料 Application ...
Android-Context-Menu.Android.zip,你可以很容易地添加令人敬畏的动画上下文菜单到你的应用程序。,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有...
android context理解
android Context Menu With Icon
android demo,BaseAdapter传递context的应用,
主要介绍了Android编程获取全局Context的方法,实例分析了基于Application类获取全局Context的实现步骤与相关技巧,需要的朋友可以参考下
Context-Menu.Android源码,是一个很不错的C++代码,有兴趣的伙伴们抽时间可以看一下把。