Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 20th, 2012  |  syntax: None  |  size: 4.46 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android App A wants to track Google Play referral data for Android App B installation
  2. <application
  3.         android:icon="@drawable/ic_launcher"
  4.         android:label="@string/app_name"
  5.         android:debuggable="true" >
  6.         <activity
  7.             android:name=".LocateMeActivity"
  8.             android:label="@string/app_name" >
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.        <receiver android:name="com.locateme.android.ReferralReceiver" android:exported="true">
  15.       <intent-filter>
  16.         <action android:name="com.android.vending.INSTALL_REFERRER" />
  17.       </intent-filter>
  18.     </receiver>
  19.     </application>
  20.        
  21. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
  22. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  23.        
  24. import java.io.UnsupportedEncodingException;
  25. import java.net.URLDecoder;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28.  
  29. import android.content.BroadcastReceiver;
  30. import android.content.Context;
  31. import android.content.Intent;
  32. import android.content.SharedPreferences;
  33. import android.os.Bundle;
  34.  
  35. public class ReferralReceiver extends BroadcastReceiver
  36. {
  37.     @Override
  38.     public void onReceive(Context context, Intent intent)
  39.     {
  40.         // Workaround for Android security issue: http://code.google.com/p/android/issues/detail?id=16006
  41.         try
  42.         {
  43.             final Bundle extras = intent.getExtras();
  44.             if (extras != null) {
  45.                 extras.containsKey(null);
  46.             }
  47.         }
  48.         catch (final Exception e) {
  49.             return;
  50.         }
  51.  
  52.         Map<String, String> referralParams = new HashMap<String, String>();
  53.  
  54.         // Return if this is not the right intent.
  55.         if (! intent.getAction().equals("com.android.vending.INSTALL_REFERRER")) { //$NON-NLS-1$
  56.             return;
  57.         }
  58.  
  59.         String referrer = intent.getStringExtra("referrer"); //$NON-NLS-1$
  60.         if( referrer == null || referrer.length() == 0) {
  61.             return;
  62.         }
  63.  
  64.         try
  65.         {    // Remove any url encoding
  66.             referrer = URLDecoder.decode(referrer, "x-www-form-urlencoded"); //$NON-NLS-1$
  67.         }
  68.         catch (UnsupportedEncodingException e) { return; }
  69.  
  70.         // Parse the query string, extracting the relevant data
  71.         String[] params = referrer.split("&"); // $NON-NLS-1$
  72.         for (String param : params)
  73.         {
  74.             String[] pair = param.split("="); // $NON-NLS-1$
  75.             referralParams.put(pair[0], pair[1]);
  76.         }
  77.  
  78.         ReferralReceiver.storeReferralParams(context, referralParams);
  79.     }
  80.  
  81.     private final static String[] EXPECTED_PARAMETERS = {
  82.         "utm_source",
  83.         "utm_medium",
  84.         "utm_term",
  85.         "utm_content",
  86.         "utm_campaign"
  87.     };
  88.  
  89.     private final static String PREFS_FILE_NAME = "ReferralParamsFile";
  90.  
  91.     public static void storeReferralParams(Context context, Map<String, String> params)
  92.     {
  93.         SharedPreferences storage = context.getSharedPreferences(ReferralReceiver.PREFS_FILE_NAME, Context.MODE_PRIVATE);
  94.         SharedPreferences.Editor editor = storage.edit();
  95.  
  96.         for(String key : ReferralReceiver.EXPECTED_PARAMETERS)
  97.         {
  98.             String value = params.get(key);
  99.             if(value != null)
  100.             {
  101.                 editor.putString(key, value);
  102.             }
  103.         }
  104.  
  105.         editor.commit();
  106.     }
  107.  
  108.  
  109.     public static Map<String, String> retrieveReferralParams(Context context)
  110.     {
  111.         HashMap<String, String> params = new HashMap<String, String>();
  112.         SharedPreferences storage = context.getSharedPreferences(ReferralReceiver.PREFS_FILE_NAME, Context.MODE_PRIVATE);
  113.  
  114.         for(String key : ReferralReceiver.EXPECTED_PARAMETERS)
  115.         {
  116.             String value = storage.getString(key, null);
  117.             if(value != null)
  118.             {
  119.                 params.put(key, value);
  120.             }
  121.         }
  122.         return params;
  123.     }
  124. }
  125.        
  126. referraltext.setText(ReferralReceiver.retrieveReferralParams(this.getApplicationContext()).toString());
  127.        
  128. am broadcast -a com.android.vending.INSTALL_REFERRER -n com.locateme.android/.ReferralReceiver --es "referrer" "utm_source=tooyoou&utm_medium=banner&utm_term=foursquare&utm_content=foursquare-tooyoou&utm_campaign=foursquare-android"
  129.        
  130. if (intent.getAction().equals(Intent.ACION_PACKAGE_ADDED))
  131.     String appName = intent.getData().toString();