我希望在MainActivity
中获取安装引荐来源值。为此,我使用两个类
1)MainActivity
2)ReferrerReceiver
我在ReferrerReceiver
课程中成功收到了install referrer值,我使用Toast控件进行了测试。
但我无法将此值从MainActivity
传递给ReferrerReceiver
。
贝娄是我试过的代码。请任何人帮助我。
感谢您提前。
1)MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv1 = (TextView) findViewById(R.id.tv1);
Intent in1= getIntent();
String ldString = in1.getStringExtra("number");
tv1.setText(ldString);
}
}
2)ReferrerReceiver.java
public class ReferrerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals("com.android.vending.INSTALL_REFERRER")) {
return;
}
Bundle bundle = intent.getExtras();
String phoneNumber = bundle.getString("referrer");
Intent i = new Intent(context, MainActivity.class);
i.putExtra("number", phoneNumber);
context.startActivity(i);
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myreferrer.in"
android:versionCode="7"
android:versionName="1.0.6" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<receiver android:name="com.myreferrer.in.ReferrerReceiver"
android:enabled="true">
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
这是我在我的项目中实施的。它为我工作。 使用此代码
(1)MainActivity.java
public class MainActivity extends Activity {
TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
}
public static void log(Context context, String string) {
if(string!=null) {
Toast.makeText(conte, "Referrer value is."+string, Toast.LENGTH_LONG).show();
tv1.setText(string);
}
}
(2)ReferrerReceiver.java
public class ReferrerReceiver extends BroadcastReceiver {
String referrer,rawReferrer;
public static String getReferrer(Context context) {
// Return any persisted referrer value or null if we don't have a referrer.
return context.getSharedPreferences("referrer", Context.MODE_PRIVATE).getString("referrer", null);
}
@Override
public void onReceive(Context context, Intent intent) {
try {
// Make sure this is the intent we expect - it always should be.
if ((null != intent) && (intent.getAction().equals("com.android.vending.INSTALL_REFERRER"))) {
// This intent should have a referrer string attached to it.
rawReferrer = intent.getStringExtra("referrer");
if (null != rawReferrer) {
// The string is usually URL Encoded, so we need to decode it.
referrer = URLDecoder.decode(rawReferrer, "UTF-8");
Toast.makeText(context, "ReferrerReceiver." + referrer, Toast.LENGTH_LONG).show();
MainActivity.log(context,"\nRaw referrer: " + rawReferrer +"\nReferrer: " + referrer);
MainActivity.log(context,referrer);
// Persist the referrer string.
context.getSharedPreferences("referrer", Context.MODE_PRIVATE).
edit().putString("referrer", referrer).commit();
} else {
Toast.makeText(context, "ReferrerReceiver." + referrer, Toast.LENGTH_LONG).show();
MainActivity.log(context,referrer);
context.getSharedPreferences("referrer", Context.MODE_PRIVATE).
edit().putString("referrer", referrer).commit();
}
} else {
Toast.makeText(context, "ReferrerReceiver intent value is null.", Toast.LENGTH_LONG).show();
MainActivity.log(context,referrer);
MainActivity.log(context,"intent value is nulll");
context.getSharedPreferences("referrer", Context.MODE_PRIVATE).
edit().putString("referrer", "intent value is nulll").commit();
}
}
catch (Exception e) {
//Toast.makeText(context, "ReferrerReceiver catch"+e, Toast.LENGTH_LONG).show();
MainActivity.log(context, e.toString());
}
}
}
(3)AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myreferrer.in"
android:versionCode="7"
android:versionName="1.0.6" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<receiver android:name="com.myreferrer.ReferrerReceiver" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER"></action>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
(4)示例链接:
https://play.google.com/store/apps/details?id=your_package_name&referrer=Welcome
答案 1 :(得分:0)
您正在使用
Intent in1 = new Intent();
String ldString = in1.getStringExtra("KEY");
你宣布错误的新意图,而你所提出的关键也是错误的,应该是number
替换为,
Intent intent= getIntent();
String ldString = intent.getStringExtra("number");
答案 2 :(得分:0)
您已在Manifest中将您的活动声明为“单个任务”;因此,如果您的
的实例活动已经启动,系统将创建(NewIntent)来重新启动你的
activity.try将此代码放在onCreate()之前:
@覆盖
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// NOW getIntent() should always return the recent
setIntent(intent);
}
使用新的附加功能获得最新的意图。