Manual integration

Follow these steps to manually integrate your Android app with Localytics.

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, integrate Localytics.
    public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // Integrate Localytics Localytics.integrate(this); } }
  4. To integrate Localytics In-app Messaging, open your main Activity class and subclass FragmentActivity instead of Activity.
    public class MainActivity extends FragmentActivity {
  5. If you are using Localytics Push Messaging, register for push notifications in onCreate().
    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
    }
  6. In onResume, open a session, start an upload, and register the activity for marketing.
    public void onResume()
    {
        super.onResume();
    
        Localytics.openSession();
        Localytics.upload();
    
        if (activity instanceof FragmentActivity)
        {
            Localytics.setInAppMessageDisplayActivity((FragmentActivity) activity);
        }
    
        Localytics.handleTestMode(activity.getIntent());
    }
  7. In onPause, close the session, upload data, and unregister the activity from marketing. onPause is the last state that's guaranteed to be called, so it makes the most sense to add the close call here. This may cause multiple close events to occur, but only the final close is honored.
    public void onPause()
    {
        if (activity instanceof FragmentActivity)
        {
            Localytics.dismissCurrentInAppMessage();
            Localytics.clearInAppMessageDisplayActivity();
        }
    
        Localytics.closeSession();
        Localytics.upload();
    
        super.onPause();
    }
  8. Repeat the above steps for every other activity in your application. Calling Localytics.openSession() in every activity's onResume will cause every activity to reconnect to the already opened session instead of creating a new one.
  9. Only 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);
    }
  10. 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
  11. 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"/>
  12. 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.