2017-11-07 8 views
1

私のアプリケーションがどこから開かれているかについて統計情報を入手したいと考えています。私はこの例を見て、の開始:ActivityからメソッドgetReferrer()を活用ディープリンクでCORRECT参照元アプリケーションを取得する

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

を。アプリケーションをデバッグしてGoogle検索アプリケーションから結果を開くときに表示されるのは、このメソッドは常に結果のhttpリンクを取得しますが、ユーザーが使用したアプリケーションがGoogle検索であるという情報は表示されません。アクティビティを調べると、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を使用し、最後の部分ができています決して使用しないでください。

呼び出し側のパッケージ名を取得する方法はありますか?

答えて

1

まあ、自分でやる方法を見つけました。誰かが変わってしまった場合のためにここで共有する。本質的には、どのようにgetReferrer()の作品を見て、これは私にパッケージ名を得るでしょう:

関連する問題