Get String array from Extra or from Meta-data through resources. - Android App

Android examples for App:Manifest

Description

Get String array from Extra or from Meta-data through resources.

Demo Code


import java.util.Locale;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

public class Main{
    private static final String TAG = "AboutUtils";
    /**//from   w  ww. j av a  2  s  .c  o  m
     * Get String array from Extra or from Meta-data through resources.
     * 
     * @param packagename
     * @param intent
     * @param extra
     * @param metadata
     */
    public static String[] getStringArrayExtraOrMetadata(
            final MetaDataReader metaDataReader, final Context context,
            final String packagename, final Intent intent,
            final String extra, final String metadata) {
        if (intent.hasExtra(extra)
                && intent.getStringArrayExtra(extra) != null) {
            return intent.getStringArrayExtra(extra);
        } else {
            //Try meta data of package
            Bundle md = metaDataReader.getBundle();

            if (md != null) {
                String[] array = null;
                try {
                    int id = md.getInt(metadata);
                    Resources resources = context.getPackageManager()
                            .getResourcesForApplication(packagename);
                    array = resources.getStringArray(id);
                } catch (NameNotFoundException e) {
                    Log.e(TAG, "Package name not found ", e);
                } catch (NumberFormatException e) {
                    Log.e(TAG, "Metadata not valid id.", e);
                } catch (Resources.NotFoundException e) {
                    Log.e(TAG, "Resource not found.", e);
                }

                if (array != null) {
                    return array;
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }
    }
}

Related Tutorials