首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正确引用深层链接的应用

正确引用深层链接的应用
EN

Stack Overflow用户
提问于 2017-11-07 15:09:02
回答 2查看 2.6K关注 0票数 3

我想得到我的应用程序是从哪里打开的统计数据。我首先看了一下这个例子:

app/client/AnalyticsApplication.java

它利用了活动中的getReferrer()方法。现在,当我调试我的应用程序并打开Google搜索应用程序的结果时,我看到的是,这个方法总是给我结果的http链接,但是它从来没有给我用户使用的应用程序是Google搜索的信息。我知道这个信息是传递给应用程序的,因为如果我检查活动,我可以看到mReferrer属性包含调用者应用程序的包名,这正是我想要的!但是,无法访问所述属性,使用它的唯一位置是在方法getReferrer()上。现在看看这个方法,下面是活动类中的内容:

代码语言:javascript
运行
复制
 /**
 * 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。

是否有任何方法可以检索调用方包名称?

EN

Stack Overflow用户

回答已采纳

发布于 2017-11-07 15:44:38

好吧,我自己想了个办法。在这里分享以防有人做同样的事。本质上,看看getReferrer()是如何工作的,这将使我获得包名:

代码语言:javascript
运行
复制
        // 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);
票数 4
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47161195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档