【发布时间】:2012-12-14 02:28:16
【问题描述】:
可能重复:
Difference between Activity Context and Application Context
何时使用上下文和活动。 我已经阅读了在构造函数中同时使用 Context 和 Activity 作为参数的代码,如下所示,请清楚我
public AmazedView(Context context, Activity activity) {
super(context);
mActivity = activity;
// init paint and make is look "nice" with anti-aliasing.
mPaint = new Paint();
mPaint.setTextSize(14);
mPaint.setTypeface(mFont);
mPaint.setAntiAlias(true);
// setup accelerometer sensor manager.
mSensorManager = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
// register our accelerometer so we can receive values.
// SENSOR_DELAY_GAME is the recommended rate for games
mSensorManager.registerListener(mSensorAccelerometer, SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_GAME);
// setup our maze and marble.
mMaze = new Maze(mActivity);
mMarble = new Marble(this);
// load array from /res/values/strings.xml
mStrings = getResources().getStringArray(R.array.gameStrings);
// set the starting state of the game.
switchGameState(GAME_INIT);
}
【问题讨论】:
-
是的@A--C 但我的问题基本上是为什么我们在构造函数中需要活动和上下文?如何在这里使用活动?
标签: java android android-activity constructor android-context