Android 如何擷取 umeng 的 渠道資訊,androidumeng

來源:互聯網
上載者:User

Android 如何擷取 umeng 的 渠道資訊,androidumeng

今天想擷取資訊清單檔的中UMENG_CHANNAL 的值但是 Debug 的時候,老是報 null.   

     <meta-data android:name="UMENG_CHANNEL" android:value="test" />

一般的方法:

<span style="font-size:18px;">/**     * 擷取渠道名     * @param ctx 此處習慣性的設定為activity,實際上context就可以     * @return 如果沒有擷取成功,那麼傳回值為空白     */    public static String getChannelName(Activity ctx) {        if (ctx == null) {            return null;        }        String channelName = null;        try {            PackageManager packageManager = ctx.getPackageManager();            if (packageManager != null) {                //注意此處為ApplicationInfo 而不是 ActivityInfo,因為友盟設定的meta-data是在application標籤中,而不是某activity標籤中,所以用ApplicationInfo                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);                if (applicationInfo != null) {                    if (applicationInfo.metaData != null) {                        channelName = applicationInfo.metaData.getString("");                    }                }            }        } catch (PackageManager.NameNotFoundException e) {            e.printStackTrace();        }        return channelName;    }</span>




抽取之後:

 

<span style="font-size:18px;"> /**     * 擷取application中指定的meta-data     * @return 如果沒有擷取成功(沒有對應值,或者異常),則傳回值為空白     */    public static String getAppMetaData(Context ctx, String key) {        if (ctx == null || TextUtils.isEmpty(key)) {            return null;        }        String resultData = null;        try {            PackageManager packageManager = ctx.getPackageManager();            if (packageManager != null) {                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);                if (applicationInfo != null) {                    if (applicationInfo.metaData != null) {                        resultData = applicationInfo.metaData.getString(key);                    }                }            }        } catch (PackageManager.NameNotFoundException e) {            e.printStackTrace();        }        return resultData;    }</span>

總結:

在 Debug模式下列印不出來渠道的資訊! 但是在發布的版本就可以 列印出資訊!


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.