首页 > 移动平台 > 详细

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

时间:2015-04-05 21:46:01      阅读:315      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!