码迷,mamicode.com
首页 > 移动开发 > 详细

Android中Context到底是什么以及用法

时间:2015-04-05 21:47:35      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

  这是Stackoverflow上一位网友的回答:

  原地址:http://stackoverflow.com/questions/3572463/what-is-context-in-android

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)

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

Typical uses of context:

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

    1 TextView tv = new TextView(getContext());
    2 ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
  • Accessing Standard Common Resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:

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

     

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

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

Android中Context到底是什么以及用法

标签:

原文地址:http://www.cnblogs.com/hua-hahaha/p/4394694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!