Android獲取清單文件中的meta-data,解決碰到數(shù)值為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,后來人大神說:讀取字符串的數(shù)值要用info.metaData.getInt,嘗試了一下,彎的佛,成功拿到,如果是數(shù)值類型的,獲取值的時候,可以采用:
info.metaData.getInt("meta_name"));
替代
info.metaData.getString("meta_name");
補充知識:android webview攔截替換本地資源,提升加載性能,節(jié)省流量
現(xiàn)在許多游戲都是直接提供一個訪問地址,然后由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;
}
還可以進一步優(yōu)化,利用webview的緩存機制,將數(shù)據(jù)緩存到本地,方法就不列出來了,網上有很多,自行百度
以上這篇Android獲取清單文件中的meta-data,解決碰到數(shù)值為null的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Android項目實戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實現(xiàn)
這篇文章主要給大家介紹了Android項目實戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實現(xiàn)的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-01-01

