Android获取清单文件中的meta-data,解决碰到数值为null的问题
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,后来人大神说:读取字符串的数值要用info.metaData.getInt,尝试了一下,弯的佛,成功拿到,如果是数值类型的,获取值的时候,可以采用:
info.metaData.getInt("meta_name"));
替代
info.metaData.getString("meta_name");
补充知识:android webview拦截替换本地资源,提升加载性能,节省流量
现在许多游戏都是直接提供一个访问地址,然后由webview去访问加载,加载速度的快慢取决于网速,当然也耗流量,这个时候,为了提高产品竞争力,产品经理就会提出需求了,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; }
还可以进一步优化,利用webview的缓存机制,将数据缓存到本地,方法就不列出来了,网上有很多,自行百度
上一篇:详解Android中使用OkHttp发送HTTP的post请求的方法
栏 目:Android代码
下一篇:Android自定义SeekBar实现视频播放进度条
本文标题:Android获取清单文件中的meta-data,解决碰到数值为null的问题
本文地址:http://www.codeinn.net/misctech/173258.html