Android 中 meta-data 用在 AndroidManifest.xml 文件中。
使用meta-data的好处是,它只需要在manifest.xml一个文件中配置,就可以在代码中的各个组件里去动态获取,实现相应的需求。集中配置的方式方便了后续的管理与修改,配合上 gradle 的 manifestPlaceholders 还可以做更加集中化的处理。
public class MainActivity extends Activity {
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.textview);
PackageManager pm = getPackageManager();
//application meta-data
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
addText(applicationInfo.metaData.getString("meta_data"));
} catch (NameNotFoundException e) {
e.printStackTrace();
}
//"activity"或"activity-alias" meta-data
try {
ActivityInfo activityInfo = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
addText(activityInfo.metaData.getString("meta_data"));
} catch (NameNotFoundException e) {
e.printStackTrace();
}
//"provider" meta-data
ComponentName providerComponentInfo = new ComponentName(this, MyContentProvider.class);
try {
ProviderInfo providerInfo = pm.getProviderInfo(providerComponentInfo, PackageManager.GET_META_DATA);
addText(providerInfo.metaData.getString("meta_data"));
} catch (NameNotFoundException e) {
e.printStackTrace();
}
//"receiver" meta-data
ComponentName receiverComponentInfo = new ComponentName(this, MyBroadcastReceiver.class);
try {
ActivityInfo receiverInfo = pm.getReceiverInfo(receiverComponentInfo, PackageManager.GET_META_DATA);
addText(receiverInfo.metaData.getString("meta_data"));
} catch (NameNotFoundException e) {
e.printStackTrace();
}
//"service" meta-data
ComponentName serviceComponentInfo = new ComponentName(this, MyService.class);
try {
ServiceInfo serviceInfo = pm.getServiceInfo(serviceComponentInfo, PackageManager.GET_META_DATA);
addText(serviceInfo.metaData.getString("meta_data"));
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
private void addText(String str) {
textview.setText(textview.getText().toString() + "\n" + str);
}
}
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
}
}
public class MyContentProvider extends ContentProvider {
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
return 0;
}
@Override
public String getType(Uri arg0) {
return null;
}
@Override
public Uri insert(Uri arg0, ContentValues arg1) {
return null;
}
@Override
public boolean onCreate() {
return false;
}
@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3, String arg4) {
return null;
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
return 0;
}
}
public class MyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
在 Manifest.xml 配置 meta-data
……
然后在 MyApplication.java 中将它们打印出来
public class MyApplication extends Applicatoin {
@Override
public void onCreate() {
super.onCreate();
String[] keys = {"mipmap", "color", "layout", "string", "number", "text"};
for(String key : keys) {
print(MetaUtil.fromApplication(this, key));
}
}
private void print(Object obj) {
System.out.println(obj + " : " + obj.getClass());
}
}
搜索关键词使用 System.out 来进行筛选,可以得出如下结论
在 meta-data 中,有个很容易让人忽略的小细节。
因此,在引入第三方SDK时,如果有全数字,且超过13位的APPID这种,很容易获取出错(最后得到的是其它数值)
我做了一个工具类,用来解决这种情况(需要被当成字符串的全数字前加一个@作为标记符)
public class MetaUtil {
/**
* 由于是自动识别类型,全数字会被识别为Integer或Float,且float和double还会自动被转为科学计数法以保留其最大精度
*/
private static String toString(Object value) {
String result = null;
if (value != null) {
String valueClassName = value.getClass().getSimpleName();
switch (valueClassName) {
case "Long":
case "Float":
case "Double":
NumberFormat numberFormat = NumberFormat.getInstance();
//不使用分组方式显示数据(即科学计数法),但这样可能会导致精度丢失,精度最多13位
numberFormat.setGroupingUsed(false);
result = numberFormat.format(value);
break;
case "String":
//当数字多于13位时(或者保守起见,只要是需要被当成字符串的全数字)都在其首位加上一个@符号
result = value.toString();
if (result.startsWith("@")) {
result = result.substring(1);
}
break;
default:
result = String.valueOf(value);
break;
}
}
return result;
}
public static Object fromApplication(Context context, String key) {
PackageManager packageManager = context.getPackageManager();
try {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
return applicationInfo.metaData.get(key);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static String fromApplicationToStr(Context context, String key) {
return toString(fromApplication(context, key));
}
public static Object fromActivity(FragmentActivity activity, String key) {
PackageManager packageManager = activity.getPackageManager();
try {
ActivityInfo activityInfo = packageManager.getActivityInfo(activity.getComponentName(), PackageManager.GET_META_DATA);
return activityInfo.metaData.get(key);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static String fromActivityToStr(FragmentActivity activity, String key) {
return toString(fromActivity(activity, key));
}
public static Object fromService(Service service, String key) {
PackageManager packageManager = service.getPackageManager();
ComponentName componentName = new ComponentName(service, service.getClass());
try {
ServiceInfo serviceInfo = packageManager.getServiceInfo(componentName, PackageManager.GET_META_DATA);
return serviceInfo.metaData.get(key);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static String fromServiceToStr(Service service, String key) {
return toString(fromService(service, key));
}
/**
* BroadcastReceiver 的 context
* 静态注册:android.app.ReceiverRestrictedContext
* 动态注册:在哪个 activity 注册的,就是那个 activity 的 context
*/
public static Object fromReceiver(Context context, BroadcastReceiver receiver, String key) {
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context, receiver.getClass());
try {
ActivityInfo serviceInfo = packageManager.getReceiverInfo(componentName, PackageManager.GET_META_DATA);
return serviceInfo.metaData.get(key);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static String fromReceiverToStr(Context context, BroadcastReceiver receiver, String key) {
return toString(fromReceiver(context, receiver, key));
}
}