【问题标题】:get CORRECT referrer application on deep linking在深度链接上获取正确的推荐人应用程序
【发布时间】:2017-11-07 15:09:02
【问题描述】:

我正在寻找有关打开我的应用程序的位置的统计信息。我开始看这个例子:

https://github.com/googlecodelabs/deeplink-referrer/blob/master/end/app/src/main/java/com/google/samples/search/recipe_app/client/AnalyticsApplication.java

它利用了来自 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


    【解决方案1】:

    好吧,我自己想出了一个办法。在这里分享,以防有人遇到同样的情况。本质上,看看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);
    

    【讨论】:

      【解决方案2】:

      在 Kotlin 中你可以使用

      referrer?.authority
      

      它会返回调用者包名

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多