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

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

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

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

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

  1. /*
  2. * Copyright Google Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License,Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing,software
  11. * distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package com.google.samples.quickstart.analytics;
  16. import android.app.Application;
  17. import com.google.android.gms.analytics.GoogleAnalytics;
  18. import com.google.android.gms.analytics.Tracker;
  19. /**
  20. * This is a subclass of {@link Application} used to provide shared objects for this app,such as
  21. * the {@link Tracker}.
  22. */
  23. public class AnalyticsApplication extends Application {
  24. private Tracker mTracker;
  25. /**
  26. * Gets the default {@link Tracker} for this {@link Application}.
  27. * @return tracker
  28. */
  29. synchronized public Tracker getDefaultTracker() {
  30. if (mTracker == null) {
  31. GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
  32. // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
  33. mTracker = analytics.newTracker(R.xml.global_tracker);
  34. }
  35. return mTracker;
  36. }
  37. }

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

正如我之前所说的,我想跟踪键盘,由于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方法.例如:

  1. public class InputMethodServiceSample extends InputMethodService {
  2. private Tracker mTracker;
  3. /**
  4. * Gets the default {@link Tracker} for this {@link Application}.
  5. * @return tracker
  6. */
  7. synchronized public Tracker getDefaultTracker() {
  8. if (mTracker == null) {
  9. GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
  10. // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
  11. mTracker = analytics.newTracker(R.xml.global_tracker);
  12. }
  13. return mTracker;
  14. }
  15. }

那么你可以在服务的每个方法中使用getDefaultTracker方法.

猜你在找的Android相关文章