本文介绍了getDefaultTracker() 来自扩展 InputMethodService 的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个适用于 Android 的键盘应用程序,它输出简单的符号而不是语言,也就是说,我希望能够跟踪用户活动,因为不涉及敏感信息或字词.

I have a keyboard app for Android that I'm developing, and it outputs simple symbols rather than language, so that said, I would love to be able to track user activity since there's no sensitive information or words involved.

问题是 Android 的 InputMethodService 没有 扩展 Application,它允许您访问 Google Analytics 的 Android SDK(可能的措辞错误,在这里,随时纠正我).

The problem is that Android's InputMethodService does not extend Application, which is what allows you to access Google Analytics' Android SDK (possible wording error, here, feel free to correct me).

我已按照指南此处开始操作,这是我引用的获取 Tracker 对象的代码:

I've followed the guide here to get started, and this is the code I referenced to acquire the Tracker object:

/*
 * Copyright Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.samples.quickstart.analytics;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

/**
 * This is a subclass of {@link Application} used to provide shared objects for this app, such as
 * the {@link Tracker}.
 */
public class AnalyticsApplication extends Application {
  private Tracker mTracker;

  /**
   * Gets the default {@link Tracker} for this {@link Application}.
   * @return tracker
   */
  synchronized public Tracker getDefaultTracker() {
    if (mTracker == null) {
      GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
      // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
      mTracker = analytics.newTracker(R.xml.global_tracker);
    }
    return mTracker;
  }
}

这对于跟踪我的应用的主要活动非常有用,它基本上只是一个包含简短说明、几个广告和一个设置快捷方式的视图.

This is all great for tracking my app's main activity, which is basically just a view containing short set of instructions with a couple of ads and a settings shortcut.

正如我之前所说,我想跟踪键盘,但由于 InputMethodService 不公开 Google Analytics,因此如何做到这一点并不十分明显.

As I said before, I'd like to track the keyboard, and how to do that isn't exactly obvious since InputMethodService doesn't expose Google Analytics.

如何在扩展 InputMethodService 但不扩展 Application 的类中使用 Google Analytics Android SDK?

How can I utilize the Google Analytics Android SDK inside of a class that extends InputMethodService but not Application?

如果我没有把我的问题说清楚,请告诉我,我会尽可能更新帖子.

Please tell me if I haven't made my issue clear, I will update the post any way I can.

推荐答案

您不必拥有 应用程序 即可使用 Google Analytics 的 Android SDK.

It's not necessary that you have an Application to use the Google Analytics' Android SDK.

该示例在 Application 类中添加了辅助方法 getDefaultTracker 以集中并简化对默认跟踪器的访问.在大多数情况下,这将是最好的解决方案,因此示例推荐了这种方法.但是有一些例外情况,此解决方案不可行,例如在 InputMethodService 中.

The example adds the helper method getDefaultTracker inside the Application class to centralize and ease the access to the default tracker. In most cases this would be the best possible solution, for this reason the example recommends this approach. But there are some exceptions where this solution is not feasible, like in the InputMethodService.

正如您在 文档方法的参数getInstance是一个Context:

As you can see in the documentation the parameter of the method getInstance is a Context:

公共静态 GoogleAnalytics getInstance(上下文上下文)

获取 GoogleAnalytics 的实例,必要时创建它.从任何线程调用这个方法都是安全的

Gets the instance of the GoogleAnalytics, creating it when necessary. It is safe to call this method from any thread

因此,您可以直接在 InputMethodService 中使用完全相同的 getDefaultTracker 方法.例如:

For this reason you can use the very same getDefaultTracker method directly inside your InputMethodService. For example:

public class InputMethodServiceSample extends InputMethodService {

    private Tracker mTracker;

    /**
    * Gets the default {@link Tracker} for this {@link Application}.
    * @return tracker
    */
    synchronized public Tracker getDefaultTracker() {
        if (mTracker == null) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
            mTracker = analytics.newTracker(R.xml.global_tracker);
        }
        return mTracker;
    }
}

然后您可以在您的服务的每个方法中使用方法 getDefaultTracker.

then you can use the methods getDefaultTracker in every method of your service.

这篇关于getDefaultTracker() 来自扩展 InputMethodService 的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:09