安卓中的Context
来源:互联网 发布:类似snapchat特效软件 编辑:程序博客网 时间:2024/05/16 06:14
Context字面意思上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄,很多方法需要通过 Context才能识别调用者的实例,比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的 实例为Activity,而到了一个button的onClick(View view)等方法时,我们用this时就会报错,所以我们可能使用ActivityName.this来解决,主要原因是因为实现Context的类主要有Android特有的几个模型,Activity、Service以及BroadcastReceiver。
Context提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型。同时启动应用级的操作,如启动Activity,broadcasting和接收intents。
protected void onCreate(Bundle state) {
super.onCreate(state);
TextView label = new TextView(this);
//传递context给view control
label.setText("Leaks are bad");
setContentView(label);
}
public class myactivity extends Activity {
private static Drawable sBackground;
protected void onCreate(Bundle state) {
super.onCreate(state);
TextView label = new TextView(this);
label.setText("Leaks are bad");
if (sBackground == null) {
sBackground = getDrawable(R.drawable.large_bitmap);
}
label.setBackgroundDrawable(sBackground);//drawable attached to a view
setContentView(label);
}
}
- 不要让生命周期长的对象引用activity context,即保证引用activity的对象要与activity本身生命周期是一样的
- 对于生命周期长的对象,可以使用application context
- 避免非静态的内部类,尽量使用静态类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化
class MyApp extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = s;
}
}
class Blah extends Activity {
@Override
public void onCreate(Bundle b){
...
MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}
下面介绍Context的一些get方法,通过这些get方法可以获取应用环境全局信息:
1.public abstract Context getApplicationContext ()
Return the context of the single, global Application object of the current process.
2.public abstract ApplicationInfo getApplicationInfo ()
Return the full application info for this context's package.
3.public abstract ContentResolver getContentResolver ()
Return a ContentResolver instance for your application's package.
4.public abstract PackageManager getPackageManager ()
Return PackageManager instance to find global package information.
5.public abstract String getPackageName ()
Return the name of this application's package.
6.public abstract Resources getResources ()
Return a Resources instance for your application's package.
7.public abstract SharedPreferences getSharedPreferences (String name, int mode)
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.
8.public final String getString (int resId)
Return a localized string from the application's package's default string table.
9.public abstract Object getSystemService (String name)
Return the handle to a system-level service by name. The class of the returned object varies by the requested name. Currently available names are:
参考博客:http://blog.chinaunix.net/space.php?uid=17102734&do=blog&id=2830227
http://blog.csdn.net/zhangqijie001/article/details/5891682
- 安卓中的Context
- 安卓中的Context
- 安卓中的context详解
- 安卓Context介绍
- 安卓Context.startService()详解
- 安卓 Context 上下文+应用共享概括
- 安卓开发:获取全局Context
- 安卓 ConText 的使用与理解
- 安卓页面跳转使用Context.startActivity注意事项
- context使用不当导致安卓内存泄露
- 安卓内存泄露之context使用不当
- context使用不当导致安卓内存泄露
- 安卓开发:全局获取Context的技巧
- 什么是Context? 怎么理解安卓上下文?(1)
- 安卓中的 ImageView
- 安卓中的hander
- 安卓中的进程
- 安卓中的dp
- 【Linux 入门笔记】命令行BASH的基本操作
- 整数的分划问题
- (基于Java)算法之合并排序
- 三点顺序(nyoj 68)
- 全面解析回溯法:算法框架与问题求解
- 安卓中的Context
- zoj 3327 Friend Number 模拟题
- android jni中C++与java互相调用小结
- DatePicker的使用(二)
- 分类算法之朴素贝叶斯分类(Naive Bayesian classification)
- IPC——共享内存与信号灯的基本函数整理
- week7_2
- 设一个长度为10的整型数组, 0)要求每个元素的值通过scanf输入,输入完成后, 1)请顺序输出这些整数, 2)请倒序输出这些整数, 3)输出这些数中的最大值, 4)输出这些数中的最小值
- Linux操作命令总结(二)