java – 来自扩展InputMethodService的类的getDefaultTracker()?

前端之家收集整理的这篇文章主要介绍了java – 来自扩展InputMethodService的类的getDefaultTracker()?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

问题是Android的InputMethodService不会扩展Application,这使您可以访问Google Analytics的Android SDK(可能的措辞错误,在这里,随时纠正我).

我已经按照指南here开始了,这是我为获取Tracker对象而引用的代码

/*
 * 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;
  }
}

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

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

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

请告诉我,如果我没有明确表达我的问题,我将以任何方式更新帖子.

最佳答案
您没有必要使用Google Analytics的Android SDK.

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

正如您在documentation中看到的,方法getInstance的参数是一个Context:

public static GoogleAnalytics getInstance (Context context)

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

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

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方法.

原文链接:https://www.f2er.com/android/429971.html

猜你在找的Android相关文章