Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

public class Main {
    private static final String TAG = "AboutUtils";

    /**
     * Get string from extra or from metadata.
     * 
     * @param context
     * @param packagename
     * @param intent
     * @param extra
     * @param metadata
     * @return
     */
    public static String getStringExtraOrMetadata(final Context context, final String packagename,
            final Intent intent, final String extra, final String metadata) {
        if (intent.hasExtra(extra) && intent.getStringExtra(extra) != null) {
            return intent.getStringExtra(extra);
        } else {
            //Try meta data of package
            Bundle md = null;
            try {
                md = context.getPackageManager().getApplicationInfo(packagename,
                        PackageManager.GET_META_DATA).metaData;
            } catch (NameNotFoundException e) {
                Log.e(TAG, "Package name not found", e);
            }

            if (md != null && !TextUtils.isEmpty(md.getString(metadata))) {
                return md.getString(metadata);
            } else {
                return "";
            }

        }
    }
}