Android获取清单文件中的meta-data,解决碰到数值为null的问题

所属分类: 软件编程 / Android 阅读数: 70
收藏 0 赞 0 分享

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获取清单文件中的meta-data,解决碰到数值为null的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android 动画之AlphaAnimation应用详解

本节讲解AlphaAnimation 动画,窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation,具体的祥看本文,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之TranslateAnimation应用详解

本节讲解TranslateAnimation动画,TranslateAnimation比较常用,比如QQ,网易新闻菜单条的动画,就可以用TranslateAnimation实现,本文将详细介绍通过TranslateAnimation 来定义动画,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之ScaleAnimation应用详解

本节讲解ScaleAnimation 动画在应用中的实现,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之RotateAnimation应用详解

本节讲解旋转动画效果RotateAnimation方法的应用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之文件操作模式深入理解

本文将介绍Android开发之文件操作模式,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android应用程序窗口(Activity)窗口对象(Window)创建指南

本文将详细介绍Android应用程序窗口(Activity)的窗口对象(Window)的创建过程,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android activity设置无标题实现全屏

本文将详细介绍Android如何设置Activity全屏和无标题的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android启动模拟器报错解决方法

本文将详细介绍Android模拟器报"Failed To Allocate memory 8"错误的解决办法,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android如何实现非本地图片的点击态

Android如何实现非本地图片的点击态,本文提供了详细的实现代码,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android viewpaper实例探讨

本文将提供一个android viewpaper实例实现过程,需要了解更多的朋友可以参考下
收藏 0 赞 0 分享
查看更多