我想得到我的应用程序是从哪里打开的统计数据。我首先看了一下这个例子:
app/client/AnalyticsApplication.java
它利用了活动中的getReferrer()
方法。现在,当我调试我的应用程序并打开Google搜索应用程序的结果时,我看到的是,这个方法总是给我结果的http链接,但是它从来没有给我用户使用的应用程序是Google搜索的信息。我知道这个信息是传递给应用程序的,因为如果我检查活动,我可以看到mReferrer
属性包含调用者应用程序的包名,这正是我想要的!但是,无法访问所述属性,使用它的唯一位置是在方法getReferrer()
上。现在看看这个方法,下面是活动类中的内容:
/**
* 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。
是否有任何方法可以检索调用方包名称?
发布于 2017-11-07 15:44:38
好吧,我自己想了个办法。在这里分享以防有人做同样的事。本质上,看看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);
https://stackoverflow.com/questions/47161195
复制相似问题