Automatic integration

Follow these steps to automatically integrate your Android app with Localytics. This approach is only supported by Ice Cream Sandwich (Android 4.0, API level 14) and later.

Once data is uploaded, it can't be deleted, so it's a good idea to create a test app in Localytics and use it to perfect your instrumentation. Then you can switch the app key to the production app.
  1. Import the class by adding the following to the top of your main activity.
    import com.localytics.android.*;
  2. If you don't have a custom Application class, create one and specify the name in your AndroidManifest.xml.
    <application
       android:name=".MyApplication"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name">
    
  3. In your Application's onCreate() method, register LocalyticsActivityLifecycleCallbacks.
    public class MyApplication extends Application
    {
       @Override
       public void onCreate()
       {
          super.onCreate();
     
          // Register LocalyticsActivityLifecycleCallbacks
          registerActivityLifecycleCallbacks(
                new LocalyticsActivityLifecycleCallbacks(this));
       }
    }
  4. In your main Activity's onCreate(), register for push notifications if you are using Localytics Push Messaging.
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
     
       // If you're using Localytics Push Messaging 
       Localytics.registerPush("YOUR_PROJECT_NUMBER");
       
       // Activity Creation Code
    }
  5. Also in your main Activity, override onNewIntent. This is used to track special Intent extras from push notifications.
    @Override
    protected void onNewIntent(Intent intent)
    {
       super.onNewIntent(intent);
       setIntent(intent);
    }
  6. In your application's manifest AndroidManifest.xml, make sure Internet permissions are available. (If your application already accesses the Internet, this will already be done.)
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />   // optional, but highly recommended
  7. For Localytics Push Messaging, take the following actions.
    1. Include the following permissions.
      <uses-permission android:name="android.permission.GET_ACCOUNTS" />
      <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
        
      <permission android:name="YOUR.PACKAGE.NAME.permission.C2D_MESSAGE"
          android:protectionLevel="signature" />
      <uses-permission android:name="YOUR.PACKAGE.NAME.permission.C2D_MESSAGE" />
      Note: Replace YOUR.PACKAGE.NAME with your package name (i.e. com.yourcompany.yourapp)
    2. Add registration for the Localytics PushReceiver to your <application> tag. Make sure to include Google Play Services in your project. More information on Google Play Services can be found here.
      <receiver
         android:name="com.localytics.android.PushReceiver"
         android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
               <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
               <action android:name="com.google.android.c2dm.intent.RECEIVE" />              
               <category android:name="YOUR.PACKAGE.NAME" />
            </intent-filter>
      </receiver>
    3. Add the PushTrackingActivity to your AndroidManifest.xml. This Activity is used to track open rates for push notifications.
      <activity android:name="com.localytics.android.PushTrackingActivity"/>
  8. For Localytics Attribution tracking, add registration for the Localytics Referral Receiver to your <application> tag.
    <receiver android:name="com.localytics.android.ReferralReceiver" android:exported="true">
          <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
          </intent-filter>
    </receiver>
Congratulations! You're integrated. Check out the data in your Dashboard or get even more insights when you start tagging events. Don't forget to test your app. Run it in an emulator, or on a real device if possible.