Android中Context简介(二)
1.首先什么是context?
文档语焉不详,说是resource什么的,反正是没看懂,实际上可以认为它是一个指向parent对象的指针,受到那个parent对象的控制。
2.为什么需要context?
试想一下这个语句:
Button myButton =newButton(this);
public class mySQLiteHelper extends SQLiteOpenHelper {..........public DBOpenHelper(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);// TODO Auto-generated constructor stub}.......}public class DBManager {//定义上面那个类对象;private mySQLiteHelper dbHelper; //注意这里得定义一个context,你想直接得到是得不到的,需要调用这个类的对象自己传一个context过来,这个context再将得到的context传到mySQLiteHelper 那个类去。private Context context; //构造方法public DBManager (Context context) {this.context = context;dbHelper = new DBOpenHelper(context,DB_NAME,null,VERSION);db = dbHelper.getWritableDatabase();}}public class myActivity extends Activity {private DBManager db; public void onCreate(Bundle savedInstanceState) {........ //dbmgr = new DataManager(getApplicationContext()); 第一种 dbmgr = new DataManager(this); //第二种//两种方法都可以,第一种是Application的context,第二种是Activity的context.........}}