Android Gets meta data from the manifest file to solve the problem encountered with a value of null

  • 2021-11-29 08:28:49
  • OfStack

1. What are meta-data? How do I get meta-data?

In AndroidManifest. xml, elements can be used as child elements, which are wrapped in activity, application, service or receiver elements. Different parent elements have different reading methods in application.

In 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");

In 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");

In 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");

In 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. Problem encountered: Getting a value of null

Before, I got the value of 1 straight key in application, but all the values obtained by 1 straight are null. Later, the Great God said: info. metaData. getInt should be used to read the value of the string. I tried 1 down, bent Buddha, and successfully got it. If it is of numerical type, when getting the value, I can use:

info.metaData.getInt("meta_name"));

Substitution

info.metaData.getString("meta_name");

Supplementary knowledge: android webview intercepts and replaces local resources to improve loading performance and save traffic

Nowadays, many games provide an access address directly. Then the loading is accessed by webview. The loading speed depends on the network speed, and of course it consumes traffic. At this time, in order to improve the competitiveness of products, the product manager will put forward the demand, and the students at the front end of web will give resources to the front end of Android. The next step is to do the processing. There are not many codes, which are used as records:


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 Main help WebView Handle various notification and request events 
  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;
}

You can also take one step to optimize, using the cache mechanism of webview to cache the data locally, and the method is not listed. There are many on the Internet, and Baidu is your own


Related articles: