getDefaultTracker() from a class that extends InputMethodService?

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

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.

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).

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.

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.

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.

回答1:

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

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.

As you can see in the documentation the parameter of the method getInstance is a 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

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

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!