2016-09-17 21 views
2

我正在開發應用程序,如果用戶使用我的應用程序成功安裝應用程序,則他會在我的應用程序中獲得獎勵 我得到信息關於接收器提供了這一點,但沒有獲得成功......
所以請幫助我得到一些全例子或其他建議的「com.android.vending.INSTALL_REFERRER」行動......如果用戶使用我的意圖播放商店成功安裝應用程序,則獲得廣播

這是我的代碼...

按鈕單擊事件

btnInstallApp.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent goToMarket = new Intent(Intent.ACTION_VIEW) 
        .setData(Uri.parse("market://details?id=com.idea.backup.smscontacts&referrer=tecksky")); 
      startActivity(goToMarket); 
     } 
    }); 

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.tecksky.referrerdemo"> 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".activity.MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <receiver 
     android:name=".receiver.ReferrerCatcher" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="com.android.vending.INSTALL_REFERRER"></action> 
     </intent-filter> 
    </receiver> 
</application> 

</manifest> 

ReferrerCatcher.java

public class ReferrerCatcher extends BroadcastReceiver { 
private static String referrer = ""; 

@Override 
public void onReceive(Context context, Intent intent) { 
    referrer = ""; 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     referrer = extras.getString("referrer"); 
    } 
    Log.e("REFERRER : ", referrer); 
    } 
} 
+0

你說的「我正在獲取信息」和「但沒有成功」是什麼意思?是否調用ReferrerCatcher.onReceive? – gus27

回答

0

嘿只看到它是如何工作我已經在我的應用程序實現的這個問題,以及和完美的適合我,所以我張貼你推薦碼:

用於接收參考創建單獨的類:

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import com.google.android.gms.analytics.CampaignTrackingReceiver; 

public class CustomReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
      new CampaignTrackingReceiver().onReceive(context, intent); 
      Bundle b= intent.getExtras(); 
      String referrerString = b.getString("referrer"); 
      // Log.e("bundle", "bundle= " + referrerString+ " " + referrerString.substring(11, referrerString.length())); 
      SharedPrefManager.setPrefVal(context, Constants.REFERRAL, referrerString.substring(11, referrerString.length()));    

    } 
} 

此課程將收到您必須保存在某處的推介,例如我將它保存在SharedPrefrence中。當用戶將從遊戲商店安裝應用程序並在"referrer"中獲得此廣播時,您將獲得哪些用戶通過此用戶的鏈接以下載應用程序的鏈接,然後從該引薦人密鑰中獲得有關引用您的應用程序的用戶的好處。

在manifest文件中添加接收器標籤

<receiver 
      android:name="gcm.CustomReceiver" 
      android:exported="true"> 
      <intent-filter> 
       <action android:name="com.android.vending.INSTALL_REFERRER" /> 
      </intent-filter> 
     </receiver> 

從後端,你會得到從服務器端的鏈接,你會打Play商店,然後像

https://play.google.com/store/apps/details?id=com.example.app&referrer=utm_source=123456ref

特定用戶

基本上在你的接收器類中,你正在選擇這個utm_source,並且你必須在用戶發送它到服務器端時將註冊你的應用程序。這意味着它會讓我們知道從哪個引用生成了哪個id。

希望這可以幫助你。試試這個,讓我知道。

+0

你好,我試圖使用你的解決方案,但是一旦應用程序啓動,我就無法讀取引用字符串。我使用'PreferenceManager.getDefaultSharedPreferences()'。什麼是您使用的'SharedPrefManager'? – 2ndGAB

+0

此處sharedprefmanager是用於在共享首選項中保存信息的自定義類。你可以自己寫。 –

+0

好的,我在其他地方看到它。這裏的關鍵是不要使用''getDefaultSharedPreferences()'''''''''''''''能夠在應用程序中讀回它,因爲它指向與應用程序不同的上下文('restrictedContext')。 – 2ndGAB

相關問題