在深度链接上获得正确的引荐来源应用程序
get CORRECT referrer application on deep linking
我希望获得有关打开我的应用程序的位置的统计信息。我开始看这个例子:
它利用了 Activity 中的方法 getReferrer()
。现在我在调试我的应用程序并从 Google 搜索应用程序打开结果时看到的是,这种方法总是让我得到结果的 http link,但它从来没有给我信息用户使用的应用是 Google 搜索。我知道此信息已传递给应用程序,因为如果我检查 Activity 我可以看到 mReferrer
属性 包含调用方应用程序的包名称,这正是我要查找的内容!但是,没有办法访问所述 属性,唯一使用它的地方是在方法 getReferrer()
上。现在看看那个方法,这就是里面的内容,来自 Activity class:
/**
* Return information about who launched this activity. If the launching Intent
* contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
* that will be returned as-is; otherwise, if known, an
* {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
* package name that started the Intent will be returned. This may return null if no
* referrer can be identified -- it is neither explicitly specified, nor is it known which
* application package was involved.
*
* <p>If called while inside the handling of {@link #onNewIntent}, this function will
* return the referrer that submitted that new intent to the activity. Otherwise, it
* always returns the referrer of the original Intent.</p>
*
* <p>Note that this is <em>not</em> a security feature -- you can not trust the
* referrer information, applications can spoof it.</p>
*/
@Nullable
public Uri getReferrer() {
Intent intent = getIntent();
try {
Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
if (referrer != null) {
return referrer;
}
String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
if (referrerName != null) {
return Uri.parse(referrerName);
}
} catch (BadParcelableException e) {
Log.w(TAG, "Cannot read referrer from intent;"
+ " intent extras contain unknown custom Parcelable objects");
}
if (mReferrer != null) {
return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
}
return null;
}
如你所见,优先权给了 Intent.EXTRA_REFERRER
,它会一直存在,所以最后使用 mReferrer 的部分永远不会被使用。
有什么方法可以检索调用程序包名称吗?
好吧,我自己想出了一个办法。在这里分享以防有人遇到同样的情况。本质上,看看 getReferrer()
是如何工作的,这会让我得到包名:
// Backup referrer info and remove it temporarily.
Uri extraReferrerBackup = getIntent().getParcelableExtra(Intent.EXTRA_REFERRER);
String extraReferrerNameBackup = getIntent().getStringExtra(Intent.EXTRA_REFERRER_NAME);
getIntent().removeExtra(Intent.EXTRA_REFERRER);
getIntent().removeExtra(Intent.EXTRA_REFERRER_NAME);
// Get the app referrer.
Uri referrer = getReferrer();
// Restore previous http referrer info.
getIntent().putExtra(Intent.EXTRA_REFERRER, extraReferrerBackup);
getIntent().putExtra(Intent.EXTRA_REFERRER_NAME, extraReferrerNameBackup);
在 Kotlin 中你可以使用
referrer?.authority
它会return调用者包名
我希望获得有关打开我的应用程序的位置的统计信息。我开始看这个例子:
它利用了 Activity 中的方法 getReferrer()
。现在我在调试我的应用程序并从 Google 搜索应用程序打开结果时看到的是,这种方法总是让我得到结果的 http link,但它从来没有给我信息用户使用的应用是 Google 搜索。我知道此信息已传递给应用程序,因为如果我检查 Activity 我可以看到 mReferrer
属性 包含调用方应用程序的包名称,这正是我要查找的内容!但是,没有办法访问所述 属性,唯一使用它的地方是在方法 getReferrer()
上。现在看看那个方法,这就是里面的内容,来自 Activity class:
/**
* Return information about who launched this activity. If the launching Intent
* contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
* that will be returned as-is; otherwise, if known, an
* {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
* package name that started the Intent will be returned. This may return null if no
* referrer can be identified -- it is neither explicitly specified, nor is it known which
* application package was involved.
*
* <p>If called while inside the handling of {@link #onNewIntent}, this function will
* return the referrer that submitted that new intent to the activity. Otherwise, it
* always returns the referrer of the original Intent.</p>
*
* <p>Note that this is <em>not</em> a security feature -- you can not trust the
* referrer information, applications can spoof it.</p>
*/
@Nullable
public Uri getReferrer() {
Intent intent = getIntent();
try {
Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
if (referrer != null) {
return referrer;
}
String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
if (referrerName != null) {
return Uri.parse(referrerName);
}
} catch (BadParcelableException e) {
Log.w(TAG, "Cannot read referrer from intent;"
+ " intent extras contain unknown custom Parcelable objects");
}
if (mReferrer != null) {
return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
}
return null;
}
如你所见,优先权给了 Intent.EXTRA_REFERRER
,它会一直存在,所以最后使用 mReferrer 的部分永远不会被使用。
有什么方法可以检索调用程序包名称吗?
好吧,我自己想出了一个办法。在这里分享以防有人遇到同样的情况。本质上,看看 getReferrer()
是如何工作的,这会让我得到包名:
// Backup referrer info and remove it temporarily.
Uri extraReferrerBackup = getIntent().getParcelableExtra(Intent.EXTRA_REFERRER);
String extraReferrerNameBackup = getIntent().getStringExtra(Intent.EXTRA_REFERRER_NAME);
getIntent().removeExtra(Intent.EXTRA_REFERRER);
getIntent().removeExtra(Intent.EXTRA_REFERRER_NAME);
// Get the app referrer.
Uri referrer = getReferrer();
// Restore previous http referrer info.
getIntent().putExtra(Intent.EXTRA_REFERRER, extraReferrerBackup);
getIntent().putExtra(Intent.EXTRA_REFERRER_NAME, extraReferrerNameBackup);
在 Kotlin 中你可以使用
referrer?.authority
它会return调用者包名