Example usage for android.content Intent getStringExtra

List of usage examples for android.content Intent getStringExtra

Introduction

In this page you can find the example usage for android.content Intent getStringExtra.

Prototype

public String getStringExtra(String name) 

Source Link

Document

Retrieve extended data from the intent.

Usage

From source file:Main.java

/**
 * Get resource ID from extra or from metadata.
 * /*from   w  ww.  j  a v  a  2  s.  c  o m*/
 * @param context
 * @param packagename
 * @param intent
 * @param extra
 * @param metadata
 * @return
 */
public static int getResourceIdExtraOrMetadata(final Context context, final String packagename,
        final Intent intent, final String extra, final String metadata) {
    if (intent.hasExtra(extra) && intent.getStringExtra(extra) != null) {

        int id = 0;
        try {
            String resourcename = intent.getStringExtra(extra);
            Resources resources = context.getPackageManager().getResourcesForApplication(packagename);
            Log.i(TAG, "Looking up resource Id for " + resourcename);
            id = resources.getIdentifier(resourcename, "", packagename);
        } catch (NameNotFoundException e) {
            Log.e(TAG, "Package name not found", e);
        }
        return id;
    } 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) {
            // Obtain resource ID and convert to resource name:
            int id = md.getInt(metadata);

            return id;
        } else {
            return 0;
        }

    }
}

From source file:Main.java

public static String getAppName(Context context, Intent appIntent) {

    if (appIntent.hasExtra(Intent.EXTRA_SHORTCUT_NAME)) {
        return appIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
    }/*from  w  w w . j av  a  2  s .  c  o  m*/

    if (appIntent.hasExtra(Intent.EXTRA_SHORTCUT_INTENT)) {
        appIntent = appIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
    }
    ComponentName componentName = appIntent.getComponent();

    PackageManager pm = context.getPackageManager();

    ApplicationInfo appInfo = null;
    ActivityInfo activityInfo = null;
    try {
        appInfo = pm.getApplicationInfo(componentName.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        appInfo = null;
    }
    try {
        activityInfo = pm.getActivityInfo(componentName, 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    if (appInfo == null) {
        return null;
    } else {
        CharSequence appName = pm.getApplicationLabel(appInfo);
        CharSequence activityName = null;

        if (activityInfo != null) {
            activityName = activityInfo.loadLabel(pm);
        }

        if (activityName != null) {
            return activityName.toString();
        }

        if (appName != null) {
            appName.toString();
        }

        return null;
    }
}

From source file:com.freshplanet.nativeExtensions.Extension.java

public static String getParametersFromIntent(Intent intent) {
    JSONObject paramsJson = new JSONObject();
    Bundle bundle = intent.getExtras();//from   w  ww . j  a  v a2s.com
    String parameters = intent.getStringExtra("parameters");
    try {
        for (String key : bundle.keySet()) {
            paramsJson.put(key, bundle.getString(key));
        }

        if (parameters != null) {
            paramsJson.put("parameters", new JSONObject(parameters));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return paramsJson.toString();
}

From source file:Main.java

/**
 * Extracts the {@link Uri url} out of an attachment upload. The url is part of the result {@link Intent} in {@link Activity#onActivityResult()} of a former
 * {@link #startAttachmentActivity()} call.
 * /*  w w w .ja  v a2  s  .com*/
 * @param activityResultIntent
 *            Result {@link Intent} from {@link Activity#onActivityResult(int, int, Intent)}.
 * @return the upload url as {@link Uri} or null, if the result {@link Intent} contains no url.
 */
public static Uri getUrlFromResult(Intent activityResultIntent) {
    Uri url = activityResultIntent.getData();
    if (url == null) {
        if (activityResultIntent.hasExtra(EXTRAS_URL)) {
            url = Uri.parse(activityResultIntent.getStringExtra(EXTRAS_URL));
        }
    }
    return url;
}

From source file:com.google.ipc.invalidation.ticl.android.c2dm.C2DMObserver.java

/**
 * Creates a new observer from the extra values contained in the provided intent.
 *//* www . ja v  a  2  s  .co  m*/
static C2DMObserver createFromIntent(Intent intent) {
    String canonicalClassString = intent.getStringExtra(C2DMessaging.EXTRA_CANONICAL_CLASS);
    try {
        // Extract observer state from the intent built by the C2DM manager.
        Class<? extends Service> clazz = Class.forName(canonicalClassString).asSubclass(Service.class);
        String filterKey = intent.getStringExtra(C2DMessaging.EXTRA_FILTER_KEY);
        String filterValue = intent.getStringExtra(C2DMessaging.EXTRA_FILTER_VALUE);
        boolean handleWakeLock = intent.getBooleanExtra(C2DMessaging.EXTRA_HANDLE_WAKELOCK, false);
        return new C2DMObserver(clazz, filterKey, filterValue, handleWakeLock);
    } catch (ClassNotFoundException e) {
        Log.e(TAG, "Unable to register observer class " + canonicalClassString);
        return null;
    }
}

From source file:gxu.software_engineering.market.android.util.ServiceHelper.java

public static void doing(ContentResolver contentResolver, Intent intent) throws JSONException {
    String httpUri = intent.getStringExtra(C.HTTP_URI);
    Log.i("http uri", httpUri);
    JSONObject data = null;/*  w w w.j  av a  2  s  . c o m*/
    try {
        data = RESTMethod.get(httpUri);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.i("json result", data.toString());
    JSONArray array = null;
    ContentValues[] items = null;
    switch (intent.getIntExtra(C.TARGET_ENTITY, -1)) {
    case CATEGORIES:
        array = data.getJSONArray(C.CATEGORIES);
        ContentValues[] categories = Processor.toCategories(array);
        contentResolver.bulkInsert(intent.getData(), categories);
        break;
    case LASTEST_USERS:
        array = data.getJSONArray(C.USERS);
        ContentValues[] users = Processor.toUsers(array);
        contentResolver.bulkInsert(intent.getData(), users);
        break;
    case LASTEST_ITEMS:
        array = data.getJSONArray(C.ITEMS);
        items = Processor.toItems(array);
        contentResolver.bulkInsert(intent.getData(), items);
        break;
    case HOTTEST_ITEMS:
        array = data.getJSONArray(C.ITEMS);
        items = Processor.toItems(array);
        contentResolver.bulkInsert(intent.getData(), items);
        break;
    case USER_ITEMS:
        array = data.getJSONArray(C.ITEMS);
        items = Processor.toItems(array);
        contentResolver.bulkInsert(intent.getData(), items);
        break;
    case CATEGORY_ITEMS:
        array = data.getJSONArray(C.ITEMS);
        items = Processor.toItems(array);
        contentResolver.bulkInsert(intent.getData(), items);
        break;
    case USER_CLOSED_ITEMS:
        array = data.getJSONArray(C.ITEMS);
        items = Processor.toItems(array);
        contentResolver.bulkInsert(intent.getData(), items);
        break;
    case USER_DEAL_ITEMS:
        array = data.getJSONArray(C.ITEMS);
        items = Processor.toItems(array);
        contentResolver.bulkInsert(intent.getData(), items);
        break;
    default:
        throw new IllegalArgumentException("sorry, 404 for the target!");
    }
}

From source file:com.otaupdater.utils.RomInfo.java

public static RomInfo fromIntent(Intent i) {
    return new RomInfo(i.getStringExtra("rom_info_name"), i.getStringExtra("rom_info_version"),
            i.getStringExtra("rom_info_changelog"), i.getStringExtra("rom_info_url"),
            i.getStringExtra("rom_info_md5"), Utils.parseDate(i.getStringExtra("rom_info_date")));
}

From source file:com.otaupdater.utils.KernelInfo.java

public static KernelInfo fromIntent(Intent i) {
    return new KernelInfo(i.getStringExtra("kernel_info_name"), i.getStringExtra("kernel_info_version"),
            i.getStringExtra("kernel_info_changelog"), i.getStringExtra("kernel_info_url"),
            i.getStringExtra("kernel_info_md5"), Utils.parseDate(i.getStringExtra("kernel_info_date")));
}

From source file:com.appsimobile.appsii.iab.IabPurchaseHelper.java

/**
 * Handles an activity result that's part of the purchase flow in in-app billing. If you
 * are calling {@link #launchPurchaseFlow}, then you must call this method from your
 * Activity's {@link android.app.Activity@onActivityResult} method. This method
 * MUST be called from the UI thread of the Activity.
 *
 * @param resultCode The resultCode as you received it.
 * @param data The data (Intent) as you received it.
 *
 * @return Returns true if the result was related to a purchase flow and was handled;
 * false if the result was not related to a purchase, in which case you should
 * handle it normally./*from   w w  w .j a  va  2  s. com*/
 */
public static int handleActivityResult(int resultCode, Intent data,
        OnIabPurchaseFinishedListener purchaseListener, String itemType, String devPayload) {

    if (data == null) {
        logError("Null data in IAB activity result.");
        return IABHELPER_BAD_RESPONSE;
    }

    int responseCode = getResponseCodeFromIntent(data);
    String purchaseData = data.getStringExtra(RESPONSE_INAPP_PURCHASE_DATA);
    String dataSignature = data.getStringExtra(RESPONSE_INAPP_SIGNATURE);

    if (resultCode == Activity.RESULT_OK && responseCode == BILLING_RESPONSE_RESULT_OK) {
        logDebug("Successful resultcode from purchase activity.");
        logDebug("Purchase data: " + purchaseData);
        logDebug("Data signature: " + dataSignature);
        logDebug("Extras: " + data.getExtras());
        logDebug("Expected item type: " + itemType);

        if (purchaseData == null || dataSignature == null) {
            logError("BUG: either purchaseData or dataSignature is null.");
            logDebug("Extras: " + data.getExtras().toString());
            return IABHELPER_UNKNOWN_ERROR;
        }

        Purchase purchase;
        try {
            purchase = new Purchase(itemType, purchaseData, dataSignature);
            String sku = purchase.getSku();

            // Verify signature
            if (!Security.verifyPurchase(RSA_CODE, purchaseData, dataSignature)) {
                logError("Purchase signature verification FAILED for sku " + sku);
                return IABHELPER_VERIFICATION_FAILED;
            }
            logDebug("Purchase signature successfully verified.");
            if (!Security.verifyDeveloperPayload(purchase, devPayload)) {
                logError("Purchase payload verification FAILED for sku " + sku);
                return IABHELPER_DEVELOPER_PAYLOAD_FAILED;
            }
            logDebug("Developer payload successfully verified.");
        } catch (JSONException e) {
            logError("Failed to parse purchase data.");
            e.printStackTrace();
            return IABHELPER_BAD_RESPONSE;
        }

        if (purchaseListener != null) {
            purchaseListener.onIabPurchaseSuccess(purchase);
        }
        return BILLING_RESPONSE_RESULT_OK;

    } else if (resultCode == Activity.RESULT_OK) {
        // result code was OK, but in-app billing response was not OK.
        logDebug("Result code was OK but in-app billing response was not OK: " + getResponseDesc(responseCode));
        return responseCode;
    } else if (resultCode == Activity.RESULT_CANCELED) {
        logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
        return IABHELPER_USER_CANCELLED;
    } else {
        logError("Purchase failed. Result code: " + Integer.toString(resultCode) + ". Response: "
                + getResponseDesc(responseCode));
        return IABHELPER_UNKNOWN_PURCHASE_RESPONSE;
    }
}

From source file:com.facebook.AppLinkData.java

/**
 * Parses out any app link data from the Intent of the Activity passed in.
 * @param activity Activity that was started because of an app link
 * @return AppLinkData if found. null if not.
 *///from   ww w.ja v a  2 s  . c  o  m
public static AppLinkData createFromActivity(Activity activity) {
    Validate.notNull(activity, "activity");
    Intent intent = activity.getIntent();
    if (intent == null) {
        return null;
    }

    AppLinkData appLinkData = createFromAlApplinkData(intent);
    if (appLinkData == null) {
        String appLinkArgsJsonString = intent.getStringExtra(BUNDLE_APPLINK_ARGS_KEY);
        appLinkData = createFromJson(appLinkArgsJsonString);
    }
    if (appLinkData == null) {
        // Try regular app linking
        appLinkData = createFromUri(intent.getData());
    }

    return appLinkData;
}