国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Android獲取清單文件中的meta-data,解決碰到數(shù)值為null的問題

瀏覽:94日期:2022-09-25 15:11:04

1.meta-data是什么?如何獲取meta-data?

在AndroidManifest.xml中,元素可以作為子元素,被包在activity、application 、service、或者receiver元素中,不同的父元素,在應用時讀取的方法也不同。

在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');

補充知識:android webview攔截替換本地資源,提升加載性能,節(jié)省流量

現(xiàn)在許多游戲都是直接提供一個訪問地址,然后由webview去訪問加載,加載速度的快慢取決于網(wǎng)速,當然也耗流量,這個時候,為了提高產(chǎn)品競爭力,產(chǎn)品經(jīng)理就會提出需求了,web前端的同學也就會把資源給到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;}

還可以進一步優(yōu)化,利用webview的緩存機制,將數(shù)據(jù)緩存到本地,方法就不列出來了,網(wǎng)上有很多,自行百度

以上這篇Android獲取清單文件中的meta-data,解決碰到數(shù)值為null的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標簽: Android
主站蜘蛛池模板: 潍坊市| 徐闻县| 西丰县| 孟津县| 通许县| 农安县| 阿鲁科尔沁旗| 灵川县| 丰顺县| 谢通门县| 土默特左旗| 磐石市| 卢龙县| 金溪县| 乐东| 廉江市| 集安市| 平湖市| 上思县| 遂平县| 潞西市| 冕宁县| 高陵县| 安仁县| 揭东县| 邹平县| 常州市| 乌审旗| 德钦县| 顺昌县| 大庆市| 个旧市| 莱阳市| 隆安县| 门源| 固阳县| 特克斯县| 中方县| 周口市| 进贤县| 西乌|