GetDefaultTracker () from a class that extends InputMethodService?

I have an Android keyboard app that I am developing, and it displays simple characters, not a language, so I would like to be able to track user activity, as there is no important information or words there.

The problem is that the Android InputMethodService does not extend the Application , which allows you to access the Google Analytics Android SDK (possible wording in the wording here, do not hesitate to correct me).

I began to be guided here , and this is the code I referenced to get 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 great for tracking the main activity of my application, which is basically a view containing a short set of instructions with several ads and a settings shortcut.

As I said, I would like to track the keyboard, and how to do this is not entirely obvious, since InputMethodService does not disclose Google Analytics.

How to use the Google Analytics SDK for Android in a class that extends InputMethodService but not Application ?

Please tell me, if I have not made my problem clear, I will update the message in any way possible.

+5
source share
1 answer

You do not need to have Application to use the Google Analytics Android application.

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

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

public static GoogleAnalytics getInstance (context context)

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

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

 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 getDefaultTracker methods in each method of your service.

+3
source

Source: https://habr.com/ru/post/1246576/


All Articles