Android meta-data

Android 中 meta-data 用在 AndroidManifest.xml 文件中。标签是提供组件额外的数据用的,它本身就是一个键值对(Bundle),可以自定义名称和值(value或resource)。

它可以包含在以下6个组件中:

meta-data 主要有以下几个常见用途:

  • 在APP发布的时候,通过修改 meta-data,来标记不同的“发布渠道”,以方便脚本自动化修改、打包、发布。
  • 引入第三方SDK时,配置appId和appKey
  • 作为日志标记,在公共父类中获取该值并作为是否打印日志的标记,用多态化的思想控制日志,免去了冗杂的在代码中进行分别配置。
  • 与 activity-alias 配合使用,meta-data作为标记,来实现类似“拨号-联系人-短信”应用的通过同一个activity打开不同的选项卡。

基础入门 —— 从组件中获取meta-data

使用meta-data的好处是,它只需要在manifest.xml一个文件中配置,就可以在代码中的各个组件里去动态获取,实现相应的需求。集中配置的方式方便了后续的管理与修改,配合上 gradle 的 manifestPlaceholders 还可以做更加集中化的处理。

Manifest.xml




    

    
        
            
                

                
            

            
        

        
            
                

                
                
            

            
        

        

        
            
        

        
            
                
            

            
        

        
            
        
    

MainActivity.java

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);
	}
}

MyBroadcastReceiver.java

public class MyBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context arg0, Intent arg1) {
	}
}

MyContentProvider.java

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;
	}
}

MyService.java

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
}

activity_main.xml



    


进阶 —— 获取各种类型的值

在 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 来进行筛选,可以得出如下结论

  • resource中存储的都是对象的ID,整型
  • value支持 Integer、Float、Boolean、String 这四种常用类型
    Android meta-data_第1张图片

终极 —— 获取字符串

在 meta-data 中,有个很容易让人忽略的小细节。

  • meta-data 的 value 如果是全数字,则获取为数字(较长的数字,为了保持精度,获取时会被自动使用科学计数法表示)
  • 只要全数字的value中出现了一个非数字,则会被当成字符串处理

因此,在引入第三方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));
    }
}

你可能感兴趣的:(Android)