Java tutorial
//package com.java2s; //License from project: Open Source License import android.content.*; import android.content.pm.PackageManager; import android.net.Uri; import java.util.List; public class Main { private static final String URL_SCHEME = "goparche://"; private static final String OPEN_ENDPOINT = "open"; private static final String DISCOUNT_FORMAT = "?partner_user_id=%s&discount_code=%s&api_key=%s"; /** * Creates an Intent to open Parche application and pass the required information to provide a user discount * along to the app. * * @param aContext The current context. * @param aDiscountCode The discount code retrieved from the Parche server. * @param aPartnerUserID The user ID to identify the user. NOTE: Will be url-encoded this * class, DO NOT URL ENCODE before passing in. * @param aAPIKey The partner applications Parche API key. * * @return The intent to open the Parche application, or null if the app needs to be updated or installed. */ public static Intent openParcheAndRequestDiscount(Context aContext, String aDiscountCode, String aPartnerUserID, String aAPIKey) { Intent returnIntent = null; if (!parcheNeedsToBeUpdatedOrInstalled(aContext)) { String urlString = URL_SCHEME + OPEN_ENDPOINT + String.format(DISCOUNT_FORMAT, aPartnerUserID, aDiscountCode, aAPIKey); returnIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlString)); } return returnIntent; } /** * Determines if there is a version of Parche on the user's device which responds * to this URL scheme. * * @param aContext The current context. * * @return true if the application needs to be updated or installed, false if you're clear * to go ahead and open the application. */ public static boolean parcheNeedsToBeUpdatedOrInstalled(Context aContext) { return !urlCanBeHandled(aContext, URL_SCHEME + OPEN_ENDPOINT); } /******************* * PRIVATE METHODS * *******************/ private static boolean urlCanBeHandled(Context aContext, String aURLString) { PackageManager packageManager = aContext.getPackageManager(); Intent openAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(aURLString)); List activitiesCanHandle = packageManager.queryIntentActivities(openAppIntent, PackageManager.MATCH_DEFAULT_ONLY); return activitiesCanHandle.size() != 0; } }