2

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

sebasira
  • 1,739
  • 1
  • 22
  • 41

1 Answers1

1

Use

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

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

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    Have a look at this answer, it's not recommended to use it as it could depend on manufacturer implementation http://stackoverflow.com/a/6760019/337141 – Hrk Jul 27 '16 at 14:23
  • Yeah, I don't think that's a real problem or something developers should worry about – ianhanniballake Jul 27 '16 at 14:26