Android 獲取meta-data中的數據

在 Android 的 Mainfest 清單文件中,Application,Activity,Recriver,Service 的節點中都有這個的存在。不少時候咱們能夠經過 meta-data 來配置一些東西,例如咱們能夠配一個app相應的歡迎頁面跳轉後的主頁等等。java

下面直接上代碼:android

AndroidMainfest.xmlapp

複製代碼
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="me.fwl.metadatademo">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:supportsRtl="true"
10         android:theme="@style/AppTheme">
11         <!--Application的MetaData的配置-->
12         <meta-data android:name="applicationMetadataKey" android:value="applicationMetadataValue"/>
13         <activity android:name=".MainActivity">
14             <!--Activity的MetaData的配置-->
15             <meta-data android:name="activityMetadataKey" android:value="activityMetadataValue"/>
16             <intent-filter>
17                 <action android:name="android.intent.action.MAIN" />
18                 <category android:name="android.intent.category.LAUNCHER" />
19             </intent-filter>
20         </activity>
21 
22         <receiver android:name=".MainReceiver">
23             <!--Receiver的MetaData的配置-->
24             <meta-data android:name="receiverMetadataKey" android:value="receiverMetadataValue"/>
25         </receiver>
26         <service android:name=".MainService">
27             <!--Service的MetaData的配置-->
28             <meta-data android:name="serviceMetadataKey" android:value="serviceMetadataValue"/>
29         </service>
30     </application>
31 
32 </manifest>
複製代碼
MainActivity.java
複製代碼
 1 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 2 
 3     private TextView result;
 4     private Context mContext;
 5     private ComponentName cn;
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         mContext = this;
12         cn = this.getComponentName();
13         result = (TextView) findViewById(R.id.result);
14         findViewById(R.id.app).setOnClickListener(this);
15         findViewById(R.id.act).setOnClickListener(this);
16         findViewById(R.id.rec).setOnClickListener(this);
17         findViewById(R.id.ser).setOnClickListener(this);
18     }
19 
20     @Override
21     public void onClick(View v) {
22         switch (v.getId()) {
23             case R.id.app:
24                 try {
25                     ApplicationInfo appInfo = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA);
26                     String appMV = appInfo.metaData.getString("applicationMetadataKey");
27                     result.setText(appMV);
28                 } catch (PackageManager.NameNotFoundException e) {
29                     e.printStackTrace();
30                 }
31                 break;
32             case R.id.act:
33                 try {
34                     ActivityInfo actInfo = mContext.getPackageManager().getActivityInfo(cn, PackageManager.GET_META_DATA);
35                     String actMV = actInfo.metaData.getString("activityMetadataKey");
36                     result.setText(actMV);
37                 } catch (PackageManager.NameNotFoundException e) {
38                     e.printStackTrace();
39                 }
40 
41                 break;
42             case R.id.rec:
43                 ComponentName recCn = new ComponentName(mContext, MainReceiver.class);
44                 try {
45                     ActivityInfo recInfo = mContext.getPackageManager().getReceiverInfo(recCn, PackageManager.GET_META_DATA);
46                     String recMV = recInfo.metaData.getString("receiverMetadataKey");
47                     result.setText(recMV);
48                 } catch (PackageManager.NameNotFoundException e) {
49                     e.printStackTrace();
50                 }
51                 break;
52             case R.id.ser:
53                 ComponentName serCn = new ComponentName(mContext, MainService.class);
54                 try {
55                     ServiceInfo serInfo = mContext.getPackageManager().getServiceInfo(serCn, PackageManager.GET_META_DATA);
56                     String serMV = serInfo.metaData.getString("serviceMetadataKey");
57                     result.setText(serMV);
58                 } catch (PackageManager.NameNotFoundException e) {
59                     e.printStackTrace();
60                 }
61                 break;
62         }
63     }
64 }
複製代碼

至於MainActivity對應的佈局文件,還有一個Service和一個Receiver的代碼就不貼了,也沒啥意義,都是空的,佈局文件裏面就一個Textview來展現結果,和4個按鈕,沒了。各自YY吧。ide

 

代碼也就在這裏了,其實換湯不換藥,只是稍微有點區別罷了,仔細看看就懂了。佈局

相關文章
相關標籤/搜索