Java – Google Analytics – networkonmainthreadexception sent in asynctask

I have to implement Google Analytics in the application I'm working on I'm using Android studio

I'm not sure if I should send a tracker from each activity implementation, or if it's enough to execute it once in the application class, but this is another story At present, Google Analytics is implemented in the application class It tries to connect to something (although I have set dryrun), fails, and then says nothing unless I cause my application to crash Then, when I try to dispatch an event, I receive a networkonmainthreadexception error

Where is the location of the attempt to schedule events on the dryrun so that the networkonmainthreadexception will not be reduced? Shouldn't it send everything to logcat on dryrun? How should I handle it on dryrun instead of?

At first, I tried to implement it without an additional asynctask class. Suppose I dump everything to logcat, but when I first got the networkonmainthreadexception error, I implemented the asynctask class

Of logcat:

01-15 13:06:21.835    1787-1800/com.example.app W/GAV4﹕ Thread[GAThread,5,main]: Service unavailable (code=1),will retry.
01-15 13:06:26.847    1787-1808/com.example.app W/GAV4﹕ Thread[Service Reconnect,using local store.
01-15 13:07:12.695    1787-1787/com.example.app E/GAV4﹕ Thread[main,main]: Error dispatching all events on exit,giving up: android.os.networkonmainthreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
        at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java:926)
        at org.apache.http.impl.socketHttpClientConnection.shutdown(SocketHttpClientConnection.java:183)
        at org.apache.http.impl.conn.DefaultClientConnection.shutdown(DefaultClientConnection.java:150)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.shutdown(AbstractPooledConnAdapter.java:169)
        at org.apache.http.impl.conn.AbstractClientConnAdapter.abortConnection(AbstractClientConnAdapter.java:378)
        at org.apache.http.impl.client.DefaultRequestDirector.abortConnection(DefaultRequestDirector.java:1031)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:530)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
        at com.google.android.gms.analytics.h.a(UnkNown Source)
        at com.google.android.gms.analytics.h.a(UnkNown Source)
        at com.google.android.gms.analytics.ag.dispatch(UnkNown Source)
        at com.google.android.gms.analytics.w.eD(UnkNown Source)
        at com.google.android.gms.analytics.w.dispatch(UnkNown Source)
        at com.google.android.gms.analytics.x$b.run(UnkNown Source)
        at com.google.android.gms.analytics.x.dY(UnkNown Source)
        at com.google.android.gms.analytics.GoogleAnalytics.dY(UnkNown Source)
        at com.google.android.gms.analytics.ExceptionReporter.uncaughtException(UnkNown Source)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
        at dalvik.system.NativeStart.main(Native Method)

Application (relevant parts):

public class App extends Application {

private static App context = null;

private static final String PROPERTY_ID = "UA-xxxxxxxx-1";

private GoogleAnalytics analytics;

private Tracker t;

public void onCreate() {
    super.onCreate();
    if (context == null)
        context = (App) getApplicationContext();
    try {
        analytics = GoogleAnalytics.getInstance(context);
        t = analytics.newTracker(R.xml.app_tracker);
        new SendTrackerInBg(t);
    }
    catch (Exception e) {e.printStackTrace();}


}

SendTrackerInBg:

public class SendTrackerInBg extends AsyncTask<Tracker,Void,Integer> {

    SendTrackerInBg(Tracker t)
    {
        this.execute(t);
    }

    @Override
    protected Integer doInBackground(Tracker... trackers) {
        try {
            trackers[0].send(new HitBuilders.AppViewBuilder().build());
            return 0;
        }
        catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
}

app_ tracker. XML (related parts):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="ga_sessionTimeout">300</integer>

    <!-- Enable automatic Activity measurement -->
    <bool name="ga_autoActivityTracking">true</bool>

    <!-- The screen names that will appear in reports -->
    <screenName name="com.example.app.App">
        Application
    </screenName>
    <string name="ga_sampleFrequency">300.0</string>
    <string name="ga_trackingId">UA-xxxxxxxx-1</string>
    <bool name="ga_reportUncaughtExceptions">true</bool>
</resources>

analytics_ global_ config. xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="ga_dryRun">true</bool>
    <string name="ga_logLevel">verbose</string>
</resources>

List (relevant parts):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"
android:versionCode="00"
android:versionName="0.00" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<permission
    android:name="com.example.app.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<Meta-data
    android:name="com.google.android.gms.analytics.globalConfigResource"
    android:resource="@xml/analytics_global_config" />

<application
    android:name=".App"
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

UPD: Thanks @ Bart hofma, I realized that I might be confused because my application crashed When GA tries to schedule events, my application does not crash Instead, GA only tries to schedule events when my application crashes, otherwise it will remain silent I deliberately crashed my application to see what GA might say I have a custom video player that works normally on real devices, but crashes on virtual machines, so when I want my application to crash, I just go to player activity on VM

Solution

You must call Google Analytics. from the thread. Here, I'm sharing the code I'm using

Put this code in a class (mbgoogleanalyticsmanager. Java)

import android.content.Context;
 import com.google.android.gms.analytics.GoogleAnalytics;
 import com.google.android.gms.analytics.HitBuilders;
 import com.google.android.gms.analytics.Tracker;


public class MBGoogleAnalyticsManager  
{
    private static final String TAG = "MBGoogleAnalyticsManager";
    private static MBGoogleAnalyticsManager instance = null;
    public String PROPERTY_ID = "UA-28454545-2";

    private Context context;
    private Tracker dataTracker;

    public static MBGoogleAnalyticsManager getInstance(Context c) 
        {
            if(instance == null) 
                {
                    System.out.println("MBGoogleAnalyticsManager new instance is created");
                    instance = new MBGoogleAnalyticsManager(c);
                }
            else
                {
                    System.out.println("MBGoogleAnalyticsManager instance is already avaiable");
                }
            return instance;
        }

    private MBGoogleAnalyticsManager(Context c)     
        {
            //super(c);
            context = c;
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(context);   
            dataTracker = analytics.newTracker(PROPERTY_ID);
        }




      //==============================================================================
     //========================== Send a screen view =================================
    //================================================================================
    public synchronized void sendGoogleAnalyticsSreenView(String screenName)
        {               
            dataTracker.setScreenName(screenName);    // Set screen name.
            //dataTracker.setAppName("MovieBuddy");  // We can send the application name [String](Optional)
            //dataTracker.setAppVersion("2.2");     // We can send the application version [String](Optional)
            //dataTracker.setLanguage("Eng");      // We can send the language [String](Optional)
            //dataTracker.setSessionTimeout(300); // We can set the time for the session time out [Long](Optional)

            dataTracker.send(new HitBuilders.AppViewBuilder()    
                                            //.setNewSession()   // If want to start a new session (Optional)
                                            .build());
            System.out.println("Screen: "+screenName);
        }



      //==============================================================================
     //========================== Send A Click Event Hit =============================
    //================================================================================
    public synchronized void sendGoogleAnalyticsHitEvents(String screenName,String category,String action,String label)
        {       
            dataTracker.setScreenName(screenName);
            dataTracker.send(new HitBuilders.EventBuilder()         
                                            .setCategory(category) // Set the category of the event [String]
                                            .setAction(action)    // Set the action has taken for the event [String]
                                            .setLabel(label)     // Set the label of for the event [String]
                                            .build());
            System.out.println("Hit Label: "+label);
        }



      //==============================================================================
     //========================== Send A Exception Event =============================
    //================================================================================

    public synchronized void sendGoogleAnalyticsException(String screenName,String ExceptionMethodName,String ExceptionLocation,boolean ExceptionFatal)
        {   
            dataTracker.setScreenName(screenName);
            dataTracker.send(new HitBuilders.ExceptionBuilder()                                         
                                            .setDescription(ExceptionMethodName + ":" + ExceptionLocation) // Send the exception details [String]
                                                                                                          //(Never send the e.message,as it contains personal info)
                                            .setFatal(ExceptionFatal)                                    // Send true or false if fatal exception error [boolean]
                                            .build());
        }



      //==============================================================================
     //================ Send A Social Event Interaction With target ==================
    //================================================================================
    public synchronized void sendGoogleAnalyticsSocialInteractionWithTarget(String screenName,String SocialNetworkName,String SocialAction,String SocialTarget)
        {       
            dataTracker.setScreenName(screenName);
            dataTracker.send(new HitBuilders.socialBuilder()                 
                                            .setNetwork(SocialNetworkName)  // Set the Social Network Name [String]
                                            .setAction(SocialAction)       // Set the action [String]
                                            .setTarget(SocialTarget)      // Set the target [String](Not a mandatory field)
                                            .build());
            System.out.println("Target: "+SocialTarget);
        }




      //==============================================================================
     //============== Send A Social Event Interaction Without target =================
    //================================================================================

    public synchronized void sendGoogleAnalyticsSocialInteraction(String SocialNetworkName,String SocialAction)
        {       
            dataTracker.send(new HitBuilders.socialBuilder()
                                            .setNetwork(SocialNetworkName)// Set the Social Network Name [String]
                                            .setAction(SocialAction)     // Set the action [String]
                                            .build());
        }
}

Now you must call this class instance method and provide the data required to call the class, as shown below:

private void sendScreenView(final String screenName)
    {   
        new Thread(new Runnable() 
            {
                @Override
                public void run() 
                    {                           
                        analyticsManager = MBGoogleAnalyticsManager.getInstance(getApplicationContext());
                        analyticsManager.sendGoogleAnalyticsSreenView(screenName);
                    }
            }).start();
    }

And call this method:

sendScreenView(“Home Activity”);

I hope you will find it very helpful

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>