【发布时间】:2017-11-07 15:09:02
【问题描述】:
我正在寻找有关打开我的应用程序的位置的统计信息。我开始看这个例子:
它利用了来自 Activity 的方法 getReferrer()。现在我在调试我的应用程序并从谷歌搜索应用程序打开结果时看到的是,这种方法总是让我得到结果的 http 链接,但它从来没有给我用户使用的应用程序是谷歌搜索的信息.我知道此信息已传递给应用程序,因为如果我检查 Activity,我可以看到 mReferrer 属性包含调用者应用程序的包名称,这正是我正在寻找的!但是,没有办法访问该属性,唯一使用它的地方是方法getReferrer()。现在看看那个方法,这是里面的东西,来自 Activity 类:
/**
* 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 的部分永远不会被使用。
有什么方法可以找回调用者包名吗?
【问题讨论】:
标签: android deep-linking firebase-analytics