Android reads data from Androidmanifest.xml: Version number, app name, custom k-v data (Meta-data)

Source: Internet
Author: User

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.example.android_metadata"
Android:versioncode= "1"
Android:versionname= "1.0" >


<uses-sdk
Android:minsdkversion= "8"
Android:targetsdkversion= "/>"


<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<meta-data
Android:name= "App_Data"
Android:value= "App_data_content"/>


<activity
Android:name= ". Mainactivity "
Android:label= "@string/app_name" >
<meta-data
Android:name= "Activity_data"
Android:value= "Activity_data_content"/>


<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>


<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>


</manifest>


Package com.example.android_metadata;


Import Android.content.pm.ActivityInfo;
Import Android.content.pm.ApplicationInfo;
Import Android.content.pm.PackageManager;
Import android.content.pm.PackageManager.NameNotFoundException;
Import Android.os.Bundle;
Import android.support.v7.app.ActionBarActivity;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.TextView;


@SuppressWarnings ("deprecation")
public class Mainactivity extends actionbaractivity
{
Private TextView Tv_data;


@Override
protected void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Tv_data = (TextView) Findviewbyid (r.id.tv_metadata);
String version = NULL;
String appName = null;
String appData = null;
String activitydata = null;
PackageInfo info;
ApplicationInfo ApplicationInfo;
Activityinfo Activityinfo;


Try
{//AppName, Versioname
info = Getpackagemanager (). Getpackageinfo (Getpackagename (), 0);
AppName = Info.applicationInfo.loadLabel (Getpackagemanager ()). ToString ();
Version = Info.versionname;
Application Medadata Information
ApplicationInfo = Getpackagemanager (). Getapplicationinfo (Getpackagename (), Getpackagemanager (). Get_meta_data);
AppData = applicationInfo.metaData.getString ("App_Data");
Activity metadata Information
Activityinfo = Getpackagemanager (). Getactivityinfo (Getcomponentname (), packagemanager.get_meta_data);
Activitydata = activityInfo.metaData.getString ("Activity_data");
} catch (namenotfoundexception E2)
{
E2.printstacktrace ();
}


Tv_data.settext ("" + AppName + "\ n" + version + "\ n" + appData + "\ n" + activitydata);

}


@Override
public boolean Oncreateoptionsmenu (Menu menu)
{
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}


@Override
public boolean onoptionsitemselected (MenuItem Item)
{
Handle Action Bar Item clicks here. The Action Bar would
Automatically handle clicks on the Home/up button, so long
As you specify a the parent activity in Androidmanifest.xml.
int id = item.getitemid ();
if (id = = r.id.action_settings)
{
return true;
}
return super.onoptionsitemselected (item);
}
}

Android reads data from Androidmanifest.xml: Version number, app name, custom k-v data (Meta-data)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android reads data from Androidmanifest.xml: Version number, app name, custom k-v data (Meta-data)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.