---
metadata:
  - name: generator
    content: Diplodoc Platform v5.66.1
alternate:
  - https://appmetrica.yandex.com.tr/docs/en/sdk/android/analytics/android-listen.md
  - https://appmetrica.yandex.com.tr/docs/ru/sdk/android/analytics/android-listen.md
  - https://appmetrica.yandex.com.tr/docs/zh/sdk/android/analytics/android-listen.md
  - href: https://appmetrica.yandex.com.tr/docs/zh/sdk/android/analytics/android-listen.md
    type: text/markdown
    title: Markdown version
  - href: https://appmetrica.yandex.com.tr/docs/zh/llms.txt?revision=r22018908
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://appmetrica.yandex.com.tr/docs/zh/llms.txt?revision=r22018908

# 跟踪用户活动

<!-- source: zh/sdk/_includes/listen-intro.md -->
在 AppMetrica 中，会话是用户与应用交互的时间段。

**后台会话**

后台会话从 AppMetrica SDK 初始化开始。

如果在后台会话中，AppMetrica 收到 Android 的 [`onResume()`](https://developer.android.com/guide/components/activities/activity-lifecycle#onresume) 系统事件或 iOS 的 [`UIApplicationDidBecomeActiveNotification`](https://developer.apple.com/documentation/uikit/uiapplicationdidbecomeactivenotification) 系统事件，则用户会话开始。

任何在这些系统事件之前发送的事件都属于后台会话。

后台会话可能指：

- 从点击应用图标到显示应用界面的时间间隔。
- 开始提供接收推送通知的服务。

**用户会话**

用户会话开始时，AppMetrica 接收以下系统事件：

- 适用于 Android：[`onResume()`](https://developer.android.com/guide/components/activities/activity-lifecycle#onresume).
- 适用于 iOS：[`UIApplicationDidBecomeActiveNotification`](https://developer.apple.com/documentation/uikit/uiapplicationdidbecomeactivenotification).

用户会话在会话超时时间结束时终止。 会话超时时间从用户最小化应用时开始计时。

任何在上述系统事件之后发送的事件都属于用户会话。

默认情况下，AppMetrica 会将用户在应用切换到后台模式（用户最小化应用或打开系统设置）后经过 [一段显著时间](*timeout-session) 再返回应用的行为视为新会话。
<!-- endsource: zh/sdk/_includes/listen-intro.md -->

## 设置会话超时时间 {#set-session-timeout}

<!-- source: zh/sdk/_includes/timeout-notify-listen.md -->
会话超时时间是指用户最小化应用后，再次打开应用前必须经过的时间 period。 您可以在 SDK 中设置会话超时时间。

- 如果用户在超时时间结束前再次打开应用，当前会话将继续。
- 如果用户在超时后再次打开应用，将开始新的会话。
<!-- endsource: zh/sdk/_includes/timeout-notify-listen.md -->

默认情况下，会话超时时间为10秒。 参数 `sessionTimeout` 的最小可接受值为 10 秒。

要更改超时时间，在创建扩展库配置时，将秒数的值传递给 `withSessionTimeout(int sessionTimeout)` 方法。

{% list tabs group=instructions %}

- Kotlin

   ```kotlin translate=no
   // Creating an extended library configuration.
   val config = AppMetricaConfig.newConfigBuilder(API_KEY)
       // Setting the length of the session timeout.
       .withSessionTimeout(15)
       .build()
   // Initializing the AppMetrica SDK.
   AppMetrica.activate(applicationContext, config)
   ```

- Java

   ```java translate=no
   // Creating an extended library configuration.
   AppMetricaConfig config = AppMetricaConfig.newConfigBuilder(API_KEY)
           // Setting the length of the session timeout.
           .withSessionTimeout(15)
           .build();
   // Initializing the AppMetrica SDK.
   AppMetrica.activate(getApplicationContext(), config);
   ```

{% endlist %}

## 监控应用生命周期 {#listen}

如果应用程序将最低 Android 版本设置为 4.0 或更高，AppMetrica 库可以自动跟踪其生命周期。 库初始化后，调用 `AppMetrica.enableActivityAutoTracking(Application application)` 方法：

{% list tabs group=instructions %}

- Kotlin

   ```kotlin translate=no
   class YourApplication : Application() {
       override fun onCreate() {
           super.onCreate()

           // Initializing the AppMetrica SDK.
           AppMetrica.activate(...)
           // Automatic tracking user activity.
           AppMetrica.enableActivityAutoTracking(this)
       }
   }
   ```

- Java

   ```java translate=no
   public class YourApplication extends Application {
       @Override
       public void onCreate() {
           super.onCreate();

           // Initializing the AppMetrica SDK.
           AppMetrica.activate(...);
           // Automatic tracking user activity.
           AppMetrica.enableActivityAutoTracking(this);
       }
   }
   ```

{% endlist %}

{% note info %}

自动活动跟踪仅对主 API 密钥生效。 在向额外的 API 密钥发送统计数据时，使用 IReporter 接口的方法。

{% endnote %}

如果应用中最低 Android 版本低于 4.0，请使用以下方法：`AppMetrica.resumeSession(activity)` 和 `AppMetrica.pauseSession(activity)`，分别在对应的活动方法中使用，即 `onResume()` 和 `onPause()`。 使用这些方法时，请确保始终通过调用`AppMetrica.pauseSession(activity)`方法终止活动会话。 如果没有调用该方法，库会认为会话仍处于活动状态，并定期与AppMetrica服务部分进行数据交换。 这可能会导致会话时长和能耗的统计不准确。

Example:

{% list tabs group=instructions %}

- Kotlin

   ```kotlin translate=no
   class YourActivity : Activity() {
       override fun onResume() {
           super.onResume()
           AppMetrica.resumeSession(this)
       }

       override fun onPause() {
           AppMetrica.pauseSession(this)
           super.onPause()
       }
   }
   ```

- Java

   ```java translate=no
   public class YourActivity extends Activity {
       @Override
       protected void onResume() {
           super.onResume();
           AppMetrica.resumeSession(this);
       }

       @Override
       protected void onPause() {
           AppMetrica.pauseSession(this);
           super.onPause();
       }
   }
   ```

{% endlist %}

## 使用额外的API密钥

通过向额外的 API 密钥发送数据，您可以控制统计数据的访问权限。 使用报告器传输事件。 要获取更多信息，请查看[使用示例](https://appmetrica.yandex.com.tr/docs/zh/sdk/android/analytics/android-operations.md)。

为了正确追踪，请手动配置报告器，为每个报告器发送会话开始和暂停的事件。 在实现活动的 `onResume()` 和 `onPause()` 时，请在 IReporter 接口中使用 `resumeSession()` 和 `pauseSession()` 方法：

{% list tabs group=instructions %}

- Kotlin

   ```kotlin translate=no
   class YourActivity : Activity() {
       override fun onResume() {
           super.onResume()
           AppMetrica.getReporter(applicationContext, ANOTHER_API_KEY).resumeSession()
       }

       override fun onPause() {
           AppMetrica.getReporter(applicationContext, ANOTHER_API_KEY).pauseSession()
           super.onPause()
       }
   }
   ```

- Java

   ```java translate=no
   public class YourActivity extends Activity {
       @Override
       protected void onResume() {
           super.onResume();
           AppMetrica.getReporter(getApplicationContext(), ANOTHER_API_KEY).resumeSession();
       }

       @Override
       protected void onPause() {
           AppMetrica.getReporter(getApplicationContext(), ANOTHER_API_KEY).pauseSession();
           super.onPause();
       }
   }
   ```

{% endlist %}

这有助于库准确监测：

- 活跃用户数量。
- Session length.
- 应用使用频率。

<!-- source: zh/_includes/feedback-button-3.md -->
如果您没有找到所需答案，可以通过反馈表单提交您的问题。 请尽可能详细地描述问题。 如果可能，请附上截图。

<a href="../troubleshooting/feedback-new">
  <span class="button">联系支持</span>
</a>

<a href="../troubleshooting/feedback-docs">
  <span class="button">为文档提出改进建议</span>
</a>
<!-- endsource: zh/_includes/feedback-button-3.md -->

[*timeout-session]: 在SDK中设置的会话超时时间。