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