What is metadata ? And what is the use of it in android

前端 未结 3 1708
花落未央
花落未央 2020-12-02 06:09

I am new to android and I have not seen or heard about meta data before. However I google it and search about it on YouTube that it is basically a information of your object

3条回答
  •  半阙折子戏
    2020-12-02 06:39

    In Android, you can define meta-data information in your AndroidManifest.xml

    HERE IS THE DOCK LINK

    Very basic usage

    It is basically an additional option to store information that can be accessed through the entire project. In this case, is defined outside tag and inside tag.

    Defining:

    
        
    
            
    
            
    
                
                    
                    
                
    
            
    
        
    
    

    Reading:

    ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
    Bundle bundle = ai.metaData;
    String myApiKey = bundle.getString("my_test_metagadata");
    

    You can save a boolean, an int, String or float.

    It's useful for library or APIs

    Let's say that you created an API/LIB which can be used for everyone. However, for a specific procedure, you need a KEY and that KEY must be defined by the developer who will use your API. This way, you can not predict which key the developer will share.

    Using , a developer who wants to use your API/LIB can share the KEY with you. This way, you leave your API configured to read that KEY and raise an exception if the user did not define.

    try {
        ApplicationInfo ai = getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
        Bundle bundle = ai.metaData;
        String myApiKey = bundle.getString("my_test_metagadata");
    } catch (Exception e) {
        Log.e(TAG, "Dear developer. Don't forget to configure  in your AndroidManifest.xml file.");
    }
    

    One classic example is Google Ads (Admob).

    You must add following line to your AndroidManifest:

    
    
    

    This will load com.google.android.gms.version with value represented by @integer/google_play_services_version. Then, probably, Google Play Services (Admob) will read this metadata and it will be able to determine the version of Google Play Service that you used when you built your app.

    Another example

    Another usage for is when to use them to configure an Activity. This way you can pass valuable information to android about your activity, and then Android can handle your activity properly. In this case, the tag is added inside the tag.

    The first example I see is when you define a Search Activity.

    
        
            
    
                
                    
                    
                
    
            
    
            
                
                    
                
                
            
        
    
    

    Then, to get the meta-data from the activity tag, use this:

    try {
            ActivityInfo ai = getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_META_DATA);
            Bundle bundle = ai.metaData;
            if (bundle != null) {
                String apiKey = bundle.getString("apikey");
                Log.d(this.getClass().getSimpleName(), "apiKey = " + apiKey);
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            Utilities.log(this.getClass().getSimpleName(), "Failed to load meta-data, NameNotFound: " + e.getMessage());
        } catch (NullPointerException e) {
            Log.e(this.getClass().getSimpleName(), "Failed to load meta-data, NullPointer: " + e.getMessage());
        }
    

提交回复
热议问题