在Android编程中,Context类的确切含义是什么?
简单地说:
顾名思义,它是应用程序/对象当前状态的上下文。它使新创建的对象了解正在发生的事情。通常,您调用它来获取有关程序另一部分(活动和程序包/应用程序)的信息。
您可以通过调用获取上下文getApplicationContext(),getContext(),getBaseContext()或this(当在扩展,从一类Context,如应用程序,活动,服务和IntentService类)。
getApplicationContext()
getContext()
getBaseContext()
this
上下文的典型用法:
创建新对象:创建新视图,适配器,侦听器:
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, ...);