【问题标题】:What is the difference between Context and Acitivity? [duplicate]上下文和活动有什么区别? [复制]
【发布时间】: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


【解决方案1】:

Context: 是系统的句柄。上下文包含环境数据,如本地文件、数据库、... 上下文还包括许多系统服务。例如,Context 将提供对Location Service 的访问,...此外,通过上下文,您可以使用资源、访问数据库和首选项、本地数据...

Activity:从Context扩展。不仅Activity,还有许多其他的扩展Context,并且每个都有自己的用途,例如:FragmentActivityServiceWallpaperServiceActivity 中的更多详细信息,“正常” android 应用程序有活动。它就像您的应用程序当前运行的环境的句柄。Activity 可以创建 UI(用户界面)

在上面的代码中,依赖其他构造函数,你应该把contextactivity对象放入。如您所见,如果activity 取自同一个context,您可以在构造函数中使用一个参数

public AmazedView(Context context) {
   Activity activity = (Activity) context;
}

但你可以看到,它并不清楚,并且将真实物体“隐藏”在后面。因为Activity 是上下文的子类,所以以一种非正式的方式,Activity 有一些上下文没有的“额外的东西”。如果你把它作为一个上下文对象,没有人知道这个事实,并且会使代码看起来晦涩难懂。此外,例如,上下文可能来自Service,并且您不确定何时转换为Activity。所以,这里做两个参数比较合适。

更重要的是,您应该谨慎使用 Activity 作为上下文对象,因为它可能会导致内存泄漏

【讨论】:

    【解决方案2】:

    应用程序上下文是指应用程序环境和进程 它的所有组件都在运行。它允许应用程序共享数据和资源 在各种构建块之间。每当启动此应用程序的第一个组件时,就会创建应用程序上下文,无论该组件是活动、服务还是其他东西。只要您的应用程序处于活动状态,应用程序上下文就会存在。因此,它独立于活动生命周期。您可以通过以下方式轻松获得对上下文的引用 打电话

      Context.getApplicationContext() or Activity.getApplication()
    

    请记住,活动和服务已经是上下文的子类,因此它们继承 它的所有方法。

    活动通常是用户一次在设备上看到的单个屏幕。一个 应用程序通常有多个活动,用户在其中来回切换 他们。因此,Activity 是您的应用程序中最明显的部分。

    你也可以看看这个:What is 'Context' on Android?

    【讨论】:

      【解决方案3】:

      在android的开发者页面上: http://developer.android.com/reference/android/content/Context.html 它指出:

      "[A context is an] 应用程序环境全局信息的接口。这是一个抽象类,其实现由 Android 系统提供。它允许访问特定于应用程序的资源和类,以及向上调用用于应用级别的操作,例如启动活动、广播和接收意图等。”

      如页面所述,Activity 是 Context 的间接子类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-23
        • 2013-02-13
        • 2011-04-26
        • 2016-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多