Keh*_*mme 5 java android google-analytics
我有一个Android正在开发的键盘应用程序,它输出简单的符号而不是语言,所以说,我希望能够跟踪用户活动,因为没有涉及的敏感信息或单词.
问题是,Android的InputMethodService也不会延长Application,这是什么使您可以访问谷歌Analytics(分析)的Android SDK(可能用词错误,在这里,随时纠正我).
我已经按照这里的指南开始了,这是我引用的代码来获取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;
}
}
Run Code Online (Sandbox Code Playgroud)
这对于跟踪我的应用程序的主要活动非常有用,这基本上只是包含一些广告和设置快捷方式的简短说明的视图.
正如我之前所说,我想跟踪键盘,如何做到这一点并不是很明显,因为InputMethodService不会暴露谷歌分析.
如何在扩展InputMethodService但未扩展的类中使用Google Analytics(分析)Android SDK Application?
请告诉我,如果我没有明确表达我的问题,我将以任何方式更新帖子.
您不必Application使用 Google Analytics 的 Android SDK。
getDefaultTracker该示例在类中添加了辅助方法,Application以集中并简化对默认跟踪器的访问。在大多数情况下,这将是最好的解决方案,因此该示例推荐这种方法。但在某些例外情况下,此解决方案不可行,例如在InputMethodService.
正如您在文档中看到的,该方法的参数getInstance是Context:
公共静态GoogleAnalytics getInstance(上下文上下文)
获取 GoogleAnalytics 的实例,并在必要时创建它。从任何线程调用此方法都是安全的
因此,您可以直接getDefaultTracker在您的InputMethodService. 例如:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
那么你就可以在你的服务的每个方法中使用这些方法getDefaultTracker。
| 归档时间: |
|
| 查看次数: |
454 次 |
| 最近记录: |