Search code examples
broadcastreceiverandroidgoogle-analytics-firebase

Google Analytics for Android v4 - Sending Event on BroadcastReceiver


I've recently started using Google Analytics on my android app. I'd like to know how can I send and Event to GA inside onReceive method of a BroadcastReceiver. It's a SMS receiver and I'd like to tell google every time a tet message arrives.

The problem is that I don't know how can I get the tracker. Inside the activities I use:

myTracker = ((Myapplication)getApplication()).getTracker();  

But I can't use getApplication() inside the onReceive. I've tried using:

myTracker = ((Myapplication)context).getTracker();

and the app crashes when the SMS arrives.

Thank you everyone! I hope yu can help me out


Solution

  • Use

    myTracker = ((Myapplication)context.getApplicationContext()).getTracker();
    

    This ensures that the Context you are casting is the Application itself.