感谢您的回答,但仍然无法正常工作。
我创建了:
public class GAnalytics extends Application{
// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "UA-XXXX-X";
public static int GENERAL_TRACKER = 0;
public enum TrackerName {
APP_TRACKER, // Tracker used only in this app.
GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
public GAnalytics() {
super();
}
synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
: (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(
R.xml.global_tracker)
: analytics.newTracker(R.xml.ecommerce_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
}
在我的MainActivity中的OnCreate下:
String ACTIVITY_NAME = "MyApp";
// Get tracker.
Tracker t = ((GAnalytics) getApplication()).getTracker(TrackerName.APP_TRACKER);
// Set screen name.
// Where path is a String representing the screen name.
t.setScreenName(ACTIVITY_NAME);
// Send a screen view.
t.send(new HitBuilders.AppViewBuilder().build());
我在应用程序下添加了清单(android:名称...)
而我的日志:
05-16 18:27:41.705:E / AndroidRuntime(25187):致命异常:主
05-16 18:27:41.705:E / AndroidRuntime(25187):进程:com.MyApplication.Banana,PID:25187
05-16 18:27:41.705:E / AndroidRuntime(25187):java.lang.NoClassDefFoundError:com.google.android.gms.analytics.GoogleAnalytics
05-16 18:27:41.705:E / AndroidRuntime(25187):在com.MyApplication.Banana.GAnalytics.getTracker(GAnalytics.java:30)
05-16 18:27:41.705:E / AndroidRuntime(25187):位于com.MyApplication.Banana.MainActivity.onCreate(MainActivity.java:56)
05-16 18:27:41.705:E / AndroidRuntime(25187):位于android.app.Activity.performCreate(Activity.java:5231)
05-16 18:27:41.705:E / AndroidRuntime(25187):位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-16 18:27:41.705:E / AndroidRuntime(25187):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-16 18:27:41.705:E / AndroidRuntime(25187):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-16 18:27:41.705:E / AndroidRuntime(25187):at android.app.ActivityThread.access $ 800(ActivityThread.java:135)
05-16 18:27:41.705:E / AndroidRuntime(25187):位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
05-16 18:27:41.705:E / AndroidRuntime(25187):位于android.os.Handler.dispatchMessage(Handler.java:102)
05-16 18:27:41.705:E / AndroidRuntime(25187):位于android.os.Looper.loop(Looper.java:136)
05-16 18:27:41.705:E / AndroidRuntime(25187):位于android.app.ActivityThread.main(ActivityThread.java:5017)
05-16 18:27:41.705:E / AndroidRuntime(25187):at java.lang.reflect.Method.invokeNative(本机方法)
05-16 18:27:41.705:E / AndroidRuntime(25187):at java.lang.reflect.Method.invoke(Method.java:515)
05-16 18:27:41.705:E / AndroidRuntime(25187):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
05-16 18:27:41.705:E / AndroidRuntime(25187):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-16 18:27:41.705:E / AndroidRuntime(25187):在dalvik.system.NativeStart.main(本机方法)
最佳答案
如您的链接中所述,与GA相关的代码应放置在Application
类而不是Activity
内。
创建一个扩展Application
的类,如下所示:
public class MyApplication extends Application{
//the tracker related code should be place here (the enum, getTracker, mTrackers)
}
然后在
AndroidManifest.xml
中的application
标记下,用上述android:name
类替换MyApplication
。跟踪器代码应放在
Activity.oncreate
方法中,例如public class MainActivity extends Activity {
.
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Set screen name.
// Where path is a String representing the screen name.
t.setScreenName(path);
// Send a screen view.
t.send(new HitBuilders.AppViewBuilder().build());
}
.
.
.
}