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


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

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

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

    2案例代码陈列

    2.1AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.lynn.metadata"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="8" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:label="@string/app_name"
                android:name=".MetaDataMainActivity" >
                <intent-filter >
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                
                <meta-data android:name="cn.lynn.name" android:value="lynn1229"/>
                <meta-data android:name="cn.lynn.age" android:value="25"/>
                <meta-data android:name="cn.lynn.app" android:value="@string/app_name"/>
                <meta-data android:name="cn.lynn.id" android:resource="@string/app_name"/>
            </activity>
        </application>
    
    </manifest>

    2.2strings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">Activity的MetaData元数据使用</string>
    </resources>

    2.3main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name" />
    
    </LinearLayout>

    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案例效果展示


  • 相关阅读:
    获取时间毫秒数
    http地址自动检测并添加URL链接
    extjs实现选择多表自定义查询功能————前台部分(ext源码)
    items中多个checkgroup在IE6下无法完整显示
    PHP压缩文件夹(ZIP)
    初始化checkboxgroup值
    下载电驴资源
    碰撞与交换
    Actionscript中MovieClip,Sprite,Shape的区别 « 檬檬前端行
    下载的ascb文件如何使用:Flash CS4 设置方法
  • 原文地址:https://www.cnblogs.com/innosight/p/3271249.html
Copyright © 2020-2023  润新知