什么是语境的Andr​​oid? [英] What is Context in Android?

查看:94
本文介绍了什么是语境的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的程序,到底什么是上下文类又是什么用的?我读到它的开发者网站,但我无法理解清楚。

In Android programming, what exactly is a Context class and what is it used for? I read about it on the developer site, but I am unable to understand it clearly.

推荐答案

把它简单地说:

顾名思义,应用/对象的当前状态其上下文。它让新创建的对象明白什么已经持续。通常你调用它来获取有关您的程序(活动,封装/应用),另一部分信息。

As the name suggests, its the context of current state of the application/object. It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application)

您可以通过调用获取上下文 getApplicationContext()的getContext() getBaseContext()(当在活动类)。

You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in the activity class).

情况下的典型应用:

  • 创建新的对象: 创建新的观点,适配器,监听器:

  • Creating New objects: Creating new views, adapters, listeners:

TextView tv = new TextView(getContext());
ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);

  • 访问标准公共资源: 像LAYOUT_INFLATER_SERVICE服务,共享preferences:

  • Accessing Standard Common Resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:

    context.getSystemService(LAYOUT_INFLATER_SERVICE)   
    getApplicationContext().getSharedPreferences(*name*, *mode*);
    

  • 访问组件含蓄: 对于内容提供商,广播,意图

  • Accessing Components Implicitly: Regarding content providers, broadcasts, intent

    getApplicationContext().getContentResolver().query(uri, ...);
    

  • 这篇关于什么是语境的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆