상세 컨텐츠

본문 제목

android Using Meta-Data In An AndroidManifest AndroidManifest.xml에 String 추가하기

스마트기기개발관련/안드로이드 개발

by AlrepondTech 2020. 9. 22. 03:32

본문

반응형

 

 

=================================

=================================

=================================

 

 

 

 

 

 

 

출처: http://witch.tistory.com/category/Program/Android?page=1

Program/Android 2013/08/01 11:04

manifest 에 

1.

<meta-data android:name="com.example.test.metatest" 

android:value="@string/metatest" /> 선언

 

2. 

<string name="metatest">테스트</string>

 

3. 

try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
            if (ai.metaData != null) {
             String metaData = ai.metaData.getString("com.example.test.metatest");
            }
        } catch (PackageManager.NameNotFoundException e) {
            // if we can't find it in the manifest, just return null
        }

 

 

=================================

=================================

=================================

 

 

 

 

출처: 

http://blog.goooood.net/android/AndroidManifest.xml%EC%97%90%20String%20%EC%B6%94%EA%B0%80%ED%95%98%EA%B8%B0.php?title=AndroidManifest.xml%EC%97%90%C2%A0String%C2%A0%EC%B6%94%EA%B0%80%ED%95%98%EA%B8%B0&idx=185

 

AndroidManifest.xml에 String 추가하기

%중요%

AndroidManifest.xml에 메타데이터 추가하기

AndroidMafest.xml에 메타데이터는 아래와 같이 추가된다.

<meta-data android:name="key" android:value="value"/>

추가될 수 있는 위치는 application, servcie, activity태그 등 다양하다. 좀 더 자세한 위치는 아래 manifest 구조에서 meta-data로 표시된 부분을 확인하자

<manifest>
    <uses-permission />
    <permission />
    <permission-tree />
    <permission-group />
    <instrumentation />
    <uses-sdk />
    <uses-configuration />  
    <uses-feature />  
    <supports-screens />  
    <compatible-screens />  
    <supports-gl-texture />  
 
    <application>
        <activity>
            <meta-data />
        </activity>
 
        <activity-alias>
            <meta-data />
        </activity-alias>
 
        <service>
            <meta-data/>
        </service>
 
        <receiver>
            <meta-data />
        </receiver>
 
        <provider>
            <meta-data />
        </provider>
 
        <uses-library />
     
        <meta-data />
    </application>
</manifest>

application 태그에 추가된 meta-data 가져오기

ApplicationInfo ai;
String value = null;
try {
    ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
    if(ai != null){
        value = ai.metaData.getString("key");
    }
} catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
 
// value에 meta-data로 저장된 값이 저장된다
Log.i("tag", value);

service, activity에 추가된 meta-data 가져오기

아래 코드는 servcie 태그에 추가된 meta-data를 가져오는 코드이며 getServiceInfo 부분을 getActivityInfo로 수정하면 activity영역에 추가된 meta-data도 가져올 수 있다.

String value = null;
try {
    ComponentName myService = new ComponentName(this, this.getClass());
     
    ServiceInfo info = getPackageManager().getServiceInfo(myService, PackageManager.GET_META_DATA);
    if(info != null){
        Bundle data = info.metaData;                
        value = data.get("GnActUrl").toString();
    }
} catch (NameNotFoundException e) {
}
 
// value에 meta-data로 저장된 값이 저장된다
Log.i("tag", value);

 

 

 

반응형

 

728x90

 

 

=================================

=================================

=================================

 

 

 

 

출처: http://blog.iangclifton.com/2010/10/08/using-meta-data-in-an-androidmanifest/

Using Meta-Data In An AndroidManifest

Posted on October 8th, 2010 by Ian G. Clifton

Sometimes you have the need to set up some app-wide configuration information in an Android app or need to create a class that can be used in multiple projects with a generic way of setting configuration values. This is particularly useful for things like API keys that will probably be different across apps but should be accessible in the same way. There are several ways to do it, but the one I’ve come to prefer is adding a meta-data node to the AndroidManifest.xml file.

This field can be used to store a boolean, float, int, or String and is later accessed by the Bundle method for your data type (e.g., getInt()). Here is an example of how to define a value in your AndroidManifest.xml:

?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.readmetadata"
  android:versionCode="1"
  android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainMenu" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="my_api_key" android:value="mykey123" />
    </application>
    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
</manifest>

Reading this meta-data takes just a few lines of Java:

?

try {
    ApplicationInfo ai = getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
    Bundle bundle = ai.metaData;
    String myApiKey = bundle.getString("my_api_key");
} catch (NameNotFoundException e) {
    Log.e(TAG, "Failed to load meta-data, NameNotFound: " + e.getMessage());
} catch (NullPointerException e) {
    Log.e(TAG, "Failed to load meta-data, NullPointer: " + e.getMessage());        
}

Activity extends ContextWrapper which has a getPackageManager() method. That method returns the PackageManager, which is used to fetch the ApplicationInfo, passing the package name and the meta-data flag. The returned ApplicationInfo contains a field, metaData, which is actually a Bundle containing all the meta data. Line 4 fetches a String that is the same as the “android:name” parameter in the XML.

That sounds more complicated than it really is. Basically, if you have an Activity, you can fetch the Bundle that has all your meta-data from the AndroidManifest and use it throughout the app.

About Ian G. Clifton

Ian currently works as the Director of User Experience for A.R.O. in Seattle, WA where he also leads Android development. Previously, he has worked as an Android developer, web developer, and even a Satellite, Wideband, and Telemetry Systems Journeyman in the United States Air Force. He has created the Essentials of Android Application Development LiveLessons video series and has just finished his first book, Android User Interface Design: Turning Ideas and Sketches into Beautifully Designed Apps.   He spends his off time on photography, drawing, developing, and doing technical review for other Android developers. You can follow his posts on this blog or his ramblings on Twitter.

View all posts by Ian G. Clifton 

This entry was posted in Code Samples and tagged android, androidmanifest, metadata. Bookmark the permalink.

 Creating An Android Main Menu Link

Animating Android Activities 

One Response to Using Meta-Data In An AndroidManifest

  1. Thiago L. Silva says:Thanks. Now I know how to use this tag. One question. This tag is like one SharedPreferences , but It can be access by the package of program ?

  2. Thiago L. Silva
    Brazil – Pe
  3. May 17th, 2012 at 9:50 am

 

=================================

=================================

=================================

 

 

반응형


관련글 더보기

댓글 영역