Android獲取清單文件中的meta-data,解決碰到數(shù)值為null的問題
1.meta-data是什么?如何獲取meta-data?
在AndroidManifest.xml中,元素可以作為子元素,被包在activity、application 、service、或者receiver元素中,不同的父元素,在應(yīng)用時讀取的方法也不同。
在activity中:
ActivityInfo info = null; try { info = this.getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } info.metaData.getString("meta_name");
在application中:
ApplicationInfo appInfo = null; try { appInfo = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } appInfo.metaData.getString("meta_name");
在service中:
ComponentName cn = new ComponentName(this, XXXXService.class); ServiceInfo info = null; try { info = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } info.metaData.getString("meta_name");
在receiver中:
ComponentName cn = new ComponentName(getApplicationContext(), XXXXXReceiver.class); ActivityInfo info = null; try { info = getApplicationContext().getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } info.metaData.getString("meta_name");
2.遇到的問題:獲取到值為null
之前在application中獲取一直key值,但是一直獲取到的都是null,后來人大神說:讀取字符串的數(shù)值要用info.metaData.getInt,嘗試了一下,彎的佛,成功拿到,如果是數(shù)值類型的,獲取值的時候,可以采用:
info.metaData.getInt("meta_name"));
替代
info.metaData.getString("meta_name");
補(bǔ)充知識:android webview攔截替換本地資源,提升加載性能,節(jié)省流量
現(xiàn)在許多游戲都是直接提供一個訪問地址,然后由webview去訪問加載,加載速度的快慢取決于網(wǎng)速,當(dāng)然也耗流量,這個時候,為了提高產(chǎn)品競爭力,產(chǎn)品經(jīng)理就會提出需求了,web前端的同學(xué)也就會把資源給到Android前端,接下來就是要做的處理了,代碼不多,用作記錄:
package com.dxgame.demo; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log; import android.webkit.MimeTypeMap; import android.webkit.WebResourceRequest; import android.webkit.WebResourceResponse; import android.webkit.WebView; import android.webkit.WebViewClient; import java.io.InputStream; import java.util.HashMap; public class CheckLocal extends AppCompatActivity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.check_local); webView.setWebViewClient(webViewClient); } //WebViewClient主要幫助WebView處理各種通知、請求事件 private WebViewClient webViewClient = new WebViewClient() { @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { Uri uri = request.getUrl(); WebResourceResponse response = checkLocalWebResourceResponse(uri); if (response != null) { return response; } return super.shouldInterceptRequest(view, request); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public WebResourceResponse shouldInterceptRequest(WebView view, String url) { Uri uri = Uri.parse(url); WebResourceResponse response = checkLocalWebResourceResponse(uri); if (response != null) { return response; } return super.shouldInterceptRequest(view, url); } }; @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private WebResourceResponse checkLocalWebResourceResponse(Uri uri) { WebResourceResponse resourceResponse = null; String urlStr = uri.toString(); Log.i("checkUrl", urlStr); String path = uri.getPath(); if (!TextUtils.isEmpty(path)) { path = path.substring(1); } try { InputStream input = getAssets().open(path); if (input != null) { Log.i("assetsPath", path); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(path.substring(path.lastIndexOf(".") + 1)); HashMap<String, String> header = new HashMap<>(); header.put("Access-Control-Allow-Origin", "*"); header.put("Access-Control-Allow-Headers", "Content-Type"); resourceResponse = new WebResourceResponse(mimeType, "", 200, "ok", header, input); } } catch (Exception e) { e.printStackTrace(); } } return resourceResponse; }
還可以進(jìn)一步優(yōu)化,利用webview的緩存機(jī)制,將數(shù)據(jù)緩存到本地,方法就不列出來了,網(wǎng)上有很多,自行百度
以上這篇Android獲取清單文件中的meta-data,解決碰到數(shù)值為null的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android CoordinatorLayout高級用法之自定義Behavior
這篇文章主要介紹了Android CoordinatorLayout高級用法之自定義Behavior,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02VerticalBannerView仿淘寶頭條實(shí)現(xiàn)垂直輪播廣告
這篇文章主要為大家詳細(xì)介紹了VerticalBannerView仿淘寶頭條實(shí)現(xiàn)垂直輪播廣告,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08Kotlin Extension Function擴(kuò)展函數(shù)詳細(xì)介紹
Kotlin支持使用新功能擴(kuò)展類的能力,而無需通過類實(shí)現(xiàn)繼承概念或使用設(shè)計(jì)模式,如裝飾器(Decorator)。這是通過稱為擴(kuò)展功能(Extension Function)的特殊方式來完成的。因此,此功能可以有效地使代碼變得更清晰和易于閱讀,并且還可以減少代碼2023-02-02Android自定義view實(shí)現(xiàn)圓形與半圓形菜單
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)圓形與半圓形菜單的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11實(shí)例分析Android中HandlerThread線程用法
本篇文章主要給大家介紹了Android HandlerThread使用介紹以及源碼解析,有需要的朋友參考學(xué)習(xí)下吧。2017-12-12android?微信搶紅包工具AccessibilityService實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了android?微信搶紅包工具AccessibilityService實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02讓Android中RadioGroup不顯示在輸入法上面的辦法
在Android開發(fā)中,發(fā)現(xiàn)一個問題,打開輸入法導(dǎo)致下面的radioGroup的位置發(fā)生了變化,被頂?shù)搅溯斎敕ǖ纳厦妫敲丛撊绾谓鉀Q呢?下面來看看。2016-08-08代碼分析Android實(shí)現(xiàn)側(cè)滑菜單
現(xiàn)在app越來越注重用戶體驗(yàn),本文給大家分析android實(shí)現(xiàn)側(cè)滑菜單的代碼,代碼簡單易懂,感興趣的朋友一起看看吧2015-11-11