什么是Context? 怎么理解安卓上下文?(1)

    xiaoxiao2025-03-13  19

    什么是Context? 怎么理解安卓上下文?

    翻译http://stackoverflow.com/questions/3572463/what-is-context-on-android

    在安卓变成中,到底Context(上下文)类是什么,有什么用?

    简单地说,是当前程序/对象的现在的状态。Context让新建的对象知道到底是怎么一回事。你可以通过context获取关于你程序的另外一部分(activity和application)的信息。

    你可以通过调用以下的方法来获得context。

    getApplicationContext() getContext() getBaseContext() this (在activity类)

    一些典型的用法:

    创建一个新对象:创建view,adapter,listener: TextView tv = new TextView(getContext()); ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...); 访问一些公共资源,比如说: LAYOUT_INFLATER_SERVICE, SharedPreferences: context.getSystemService(LAYOUT_INFLATER_SERVICE) getApplicationContext().getSharedPreferences(*name*, *mode*); 隐式的访问一些组件:内容提供者,广播,意图 getApplicationContext().getContentResolver().query(uri, ...);
    转载请注明原文地址: https://ju.6miu.com/read-1296985.html
    最新回复(0)