Android应用开发之MetaData之数据挖掘

传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229

        今天我们学习如何为Activity、Service、BroadcastReceiver等组件设置MetaData元数据,下面给出该场景的案例:

1案例技术要点(待整理)

2案例代码陈列

2.1AndroidManifest.xml




    

    
        
            
                

                
            
            
            
            
            
            
        
    

2.2strings.xml



    Activity的MetaData元数据使用

2.3main.xml




    

2.4MetaDataMainActivity.java

package cn.lynn.metadata;

import android.app.Activity;
import android.content.ComponentName;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.widget.Toast;

public class MetaDataMainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            ActivityInfo activityInfo = this.getPackageManager().getActivityInfo(new ComponentName(this, MetaDataMainActivity.class), PackageManager.GET_META_DATA);
            Bundle bundle = activityInfo.metaData;
            String name = bundle.getString("cn.lynn.name");
            String app = bundle.getString("cn.lynn.app");
            int age = bundle.getInt("cn.lynn.age");
            int sourceid = bundle.getInt("cn.lynn.id");

            Toast.makeText(this, "name= " + name + ", age=" + age + ", app= " + app + ", sourceid= " + sourceid, Toast.LENGTH_LONG).show();
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}

3案例效果展示

Android应用开发之MetaData之数据挖掘_第1张图片


你可能感兴趣的:(Android,Develop)