android studio for android learning (二十 )android中this、context等关键概念理解全解
来源:互联网 发布:vb系列振动电机报价 编辑:程序博客网 时间:2024/06/05 02:07
1. 最近看的代码中经常看见this,在java中的this第一反应就是当前对象,可以用来引用变量或是方法,一看就很懵B,所以这里通过例子来详细讲下this的用法。
2.例如下面代码
button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {Toast.makeText(TextC.this,"什么情况",1000).show(); }
通俗讲,this就是指本类,但在上面Toast中直接写this会出错,因为当前本类是OnClickListener类,而不是我们的主类,如activity(或是mainActivity等),而this就是指向当前类OnClickListener。
3.再如:MainActivity中setOnClickListener(this)中的this指代什么?
setOnClickListener的参数要求是一个实现了OnClickListener接口的对象实体,它可以是任何类的实例,只要该类实现了OnClickListener。这个问题中,this它就是MainActivity这个对象自己且用this实现了OnClickListener。
4.MainActivity.this是什么意思?
表示的就是MainActivity这个类对象本来,这种写法一般用在内部类里,因为在外部类中直接可以用关键字this表示本类,而内部类中直接写this的话表示的是内部类本身,想表示外部类的话就得加上外部类的类名.this。
5.在android中this使用的小结:
this代表本类的一个引用,this.*表示调用本类的某个方法,这个时候通常可以省略this;但在内部类中不能省略,否则编译器会认为是内部类的引用,所以要在this前加上类名. *.this 表示本类的引用,通常前面的*是用本类的名字表示,当然也可以省略,但是如果是在内部类中一定要加上类名,同时注意:this和static不能共存,就是在static修饰的方法中不能用this.
6.android context是什么 ?从SDK中可以知道
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-cal for application-level operations such as launching activities, broadcasting and receiving intents, etc
从上的描述可以知道context和一下三点作用:
- 它描述的是一个应用程序的环境,即上下文
- 它类是一个抽象的类,android提供了一个具体的通用实现类contextIml类。
- 它就像是一个大管家,是一个访问全局信息的接口。通过它我们可以获取应用程度 的资源的类,包括一些应用级的操作,如启动一个activity,发送广播,接受Intent信息。
7.context家族的关系
8.android context源码简析
8.1Context.java:抽象类,提供了一组通用的API
public abstract class Context { ... public abstract Object getSystemService(String name); //获得系统级服务 public abstract void startActivity(Intent intent); //通过一个Intent启动Activity public abstract ComponentName startService(Intent service); //启动Service //根据文件名得到SharedPreferences对象 public abstract SharedPreferences getSharedPreferences(String name,int mode); ... }
8.2 Contextlml.java:Context和实现类,但函数的大部分功能都是直接调用其属性的mPackageInfo去完成
/** * Common implementation of Context API, which provides the base * context object for Activity and other application components. */ class ContextImpl extends Context{ //所有Application程序公用一个mPackageInfo对象 /*package*/ ActivityThread.PackageInfo mPackageInfo; @Override public Object getSystemService(String name){ ... else if (ACTIVITY_SERVICE.equals(name)) { return getActivityManager(); } else if (INPUT_METHOD_SERVICE.equals(name)) { return InputMethodManager.getInstance(this); } } @Override public void startActivity(Intent intent) { ... //开始启动一个Activity mMainThread.getInstrumentation().execStartActivity( getOuterContext(), mMainThread.getApplicationThread(), null, null, intent, -1); } }
8.3 ContextWrapper.java:该类只是对Context类的一种包装,该类的构造函数包含了一个真正的Context引用,即ContextIml对象。
public class ContextWrapper extends Context { Context mBase; //该属性指向一个ContextIml实例,一般在创建Application、Service、Activity时赋值 //创建Application、Service、Activity,会调用该方法给mBase属性赋值 protected void attachBaseContext(Context base) { if (mBase != null) { throw new IllegalStateException("Base context already set"); } mBase = base; } @Override public void startActivity(Intent intent) { mBase.startActivity(intent); //调用mBase实例方法 } }
8.4ContextThemeWrapper.java:该类内部包含了主题(Theme)相关的接口,即android:theme属性指定的。只有Activity需要主题,Service不需要主题,所以Service直接继承于ContextWrapper类。
public class ContextThemeWrapper extends ContextWrapper { //该属性指向一个ContextIml实例,一般在创建Application、Service、Activity时赋值 private Context mBase; //mBase赋值方式同样有一下两种 public ContextThemeWrapper(Context base, int themeres) { super(base); mBase = base; mThemeResource = themeres; } @Override protected void attachBaseContext(Context newBase) { super.attachBaseContext(newBase); mBase = newBase; } }
9.Activity类 、Service类 、Application类本质上都是Context子类,所以应用程序App共有的Context数目公式为:
总Context实例个数 = Service个数 + Activity个数 + 1(Application对应的Context实例)
10.AR/VR研究的朋友可以加入下面的群或是关注下面的微信公众号
0 0
- android studio for android learning (二十 )android中this、context等关键概念理解全解
- android studio for android learning (二)
- android studio for android learning (二十八) android基础知识
- android studio for android learning (二十六 )自定义控件理解与浅析(1)
- android studio for android learning (二十四 )bitmap and bitmapFactory
- android studio for android learning (十九 ) 最新Handler消息传递机制全解
- android studio for android learning (十) android之activity的启动和关闭
- android studio for android learning (一)
- android studio for android learning (四) 兼容性问题
- android中context的理解
- Android中Context的理解
- android中深入理解 Context
- android studio for android learning (六)应用程序窗体显示状态操作(requestWindowFeature等的应用)
- Android之Context、this
- android中this、getAppliaction()、context的区别。
- android studio for android learning (五) 最新Activity理解与其生命周期
- android studio for android learning (七) Android Log类全解
- android studio for android learning (九) android之Adapter用法
- MVC设计模式
- Java基础02 java的访问控制以及与C#的异同
- hadoop源码编译环境安装配置
- android.content.res.Resources$NotFoundException: Unable to find resource ID 0#0xffffffff
- HttpURLConnection+URl网络请求
- android studio for android learning (二十 )android中this、context等关键概念理解全解
- 逻辑中断
- 每日安全简讯20160719
- security information and event management 学习初探(三)
- Visual C++ 在windows 7 上预编译头错误unexpected precompiled header error, simply rerunning the compiler migh
- ajax get提交后台乱码问题
- iOS--UI之导航控制器与标签控制器
- using声明、using指示用于嵌套命名空间时的作用域
- 正经的文件分类——普通嗒