Android meta-data

Android meta-data数据的读取

meta-data元数据,当我们在集成第三方sdk时会经常遇到,比如百度地图sdk,talkingData统计等,所以做了一个小总结,以备使用。

在清单文件中,meta-data可以在Application,Activity,Service,Receiver中,在不同的位置,读取数据的方式有稍微的差别,现写了一个小demo。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.dc.metadatasummary01">

    <application  android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <!--位于activity中-->
        <meta-data  android:name="in_main_activity" android:value="I am in activity"></meta-data>
    </activity>

    <service android:name=".DCTestMetaDataService">
        <!--位于服务中-->
        <meta-data  android:name="in_service" android:value="I am in Service"></meta-data>

    </service>

    <receiver android:name=".DCTestMetaDataReceiver">
        <!--位于广播接收者中-->
        <intent-filter>
            <action android:name="dc_test_meta_data_receiver"></action>
        </intent-filter>
        <meta-data  android:name="in_receiver" android:value="I am in Receiver"></meta-data>
    </receiver>
        <!--位于application中-->
    <meta-data  android:name="in_application" android:value="I am in application"></meta-data>
</application>
</manifest>

1.Activity中拿取数据

ActivityInfo info = this.getPackageManager().getActivityInfo(getComponentName(),PackageManager.GET_META_DATA);
String msg = info.metaData.getString("in_main_activity");

2.Application中拿取数据

ApplicationInfo appInfo = this.getPackageManager().getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA);
String msg = appInfo.metaData.getString("in_application");

3.Service中拿取数据

ComponentName cn = new ComponentName(this, DCTestMetaDataService.class);
ServiceInfo info2 = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);
String msg = info2.metaData.getString("in_service");

4.Receiver中拿取数据

ComponentName cn = new ComponentName(context, DCTestMetaDataReceiver.class);
ActivityInfo info = context.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);
String msg = info.metaData.getString("in_receiver");

MainActivity代码

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //拿取activity中的元数据
        ActivityInfo info = null;
        try {
            info = this.getPackageManager()
                    .getActivityInfo(getComponentName(),
                            PackageManager.GET_META_DATA);
            String msg = info.metaData.getString("in_main_activity");
            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        //拿取application中的元数据
        ApplicationInfo appInfo = null;
        try {
            appInfo = this.getPackageManager()
                    .getApplicationInfo(getPackageName(),
                            PackageManager.GET_META_DATA);
            String msg = appInfo.metaData.getString("in_application");
            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        //开启服务
        startService(new Intent(MainActivity.this, DCTestMetaDataService.class));

        //发送广播
        Intent intent = new Intent();
        intent.setAction("dc_test_meta_data_receiver");
        sendBroadcast(intent);
    }

    @Override
    protected void onDestroy() {
        //关闭服务
        stopService(new Intent(MainActivity.this, DCTestMetaDataService.class));
        super.onDestroy();
    }

DCTestMetaDataReceiver代码


/** * @author zyc * created at 2016/4/22 18:01 */
public class DCTestMetaDataReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (TextUtils.equals(intent.getAction(), "dc_test_meta_data_receiver")) {
            ComponentName cn = new ComponentName(context, DCTestMetaDataReceiver.class);
            ActivityInfo info = null;
            try {
                info = context.getPackageManager()
                        .getReceiverInfo(cn, PackageManager.GET_META_DATA);
                String msg = info.metaData.getString("in_receiver");
                Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

DCTestMetaDataService代码

/** * @author zyc * created at 2016/4/22 11:42 */
public class DCTestMetaDataService extends Service {

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

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //拿到meta-data元数据
        ComponentName cn = new ComponentName(this, DCTestMetaDataService.class);
        ServiceInfo info2 = null;
        try {
            info2 = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);
            String msg = info2.metaData.getString("in_service");
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

ok,so easy……

你可能感兴趣的:(Manifest)