com.google.zxing.integration.android
Class IntentIntegrator

java.lang.Object
  extended by com.google.zxing.integration.android.IntentIntegrator

public final class IntentIntegrator
extends Object

A utility class which helps ease integration with Barcode Scanner via. Intents. This is a simple way to invoke barcode scanning and receive the result, without any need to integrate, modify, or learn the project's source code.

Initiating a barcode scan

Integration is essentially as easy as calling initiateScan(Activity) and waiting for the result in your app.

It does require that the Barcode Scanner application is installed. The initiateScan(Activity) method will prompt the user to download the application, if needed.

There are a few steps to using this integration. First, your Activity must implement the method Activity.onActivityResult(int, int, Intent) and include a line of code like this:

 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
 IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
 if (scanResult != null) {
 // handle scan result
 }
 // else continue with any other code you need in the method
 ...
 }
 
 

This is where you will handle a scan result. Second, just call this in response to a user action somewhere to begin the scan process:

 IntentIntegrator.initiateScan(yourActivity);
 

You can use initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence) or initiateScan(Activity, int, int, int, int) to customize the download prompt with different text labels.

Note that initiateScan(Activity) returns an AlertDialog which is non-null if the user was prompted to download the application. This lets the calling app potentially manage the dialog. In particular, ideally, the app dismisses the dialog if it's still active in its Activity.onPause() method.

Sharing text via barcode

To share text, encoded as a QR Code on-screen, similarly, see shareText(Activity, CharSequence).

Some code, particularly download integration, was contributed from the Anobiit application.

Author:
Sean Owen, Fred Lin, Isaac Potoczny-Jones, Brad Drehmer, gcstang

Field Summary
static String ALL_CODE_TYPES
          The Constant ALL_CODE_TYPES.
static String DEFAULT_MESSAGE
          The Constant DEFAULT_MESSAGE.
static String DEFAULT_NO
          The Constant DEFAULT_NO.
static String DEFAULT_TITLE
          The Constant DEFAULT_TITLE.
static String DEFAULT_YES
          The Constant DEFAULT_YES.
static String ONE_D_CODE_TYPES
          The Constant ONE_D_CODE_TYPES.
static String PRODUCT_CODE_TYPES
          The Constant PRODUCT_CODE_TYPES.
static String QR_CODE_TYPES
          The Constant QR_CODE_TYPES.
static int REQUEST_CODE
          The Constant REQUEST_CODE.
 
Method Summary
static AlertDialog initiateScan(Activity activity)
          See.
static AlertDialog initiateScan(Activity activity, CharSequence stringTitle, CharSequence stringMessage, CharSequence stringButtonYes, CharSequence stringButtonNo)
          See.
static AlertDialog initiateScan(Activity activity, CharSequence stringTitle, CharSequence stringMessage, CharSequence stringButtonYes, CharSequence stringButtonNo, CharSequence stringDesiredBarcodeFormats)
          Invokes scanning.
static AlertDialog initiateScan(Activity activity, int stringTitle, int stringMessage, int stringButtonYes, int stringButtonNo)
          See.
static IntentResult parseActivityResult(int requestCode, int resultCode, Intent intent)
           Call this from your Activity's.
static void shareText(Activity activity, CharSequence text)
          See.
static void shareText(Activity activity, CharSequence text, CharSequence stringTitle, CharSequence stringMessage, CharSequence stringButtonYes, CharSequence stringButtonNo)
          Shares the given text by encoding it as a barcode, such that another user can scan the text off the screen of the device.
static void shareText(Activity activity, CharSequence text, int stringTitle, int stringMessage, int stringButtonYes, int stringButtonNo)
          See.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REQUEST_CODE

public static final int REQUEST_CODE
The Constant REQUEST_CODE.

See Also:
Constant Field Values

DEFAULT_TITLE

public static final String DEFAULT_TITLE
The Constant DEFAULT_TITLE.

See Also:
Constant Field Values

DEFAULT_MESSAGE

public static final String DEFAULT_MESSAGE
The Constant DEFAULT_MESSAGE.

See Also:
Constant Field Values

DEFAULT_YES

public static final String DEFAULT_YES
The Constant DEFAULT_YES.

See Also:
Constant Field Values

DEFAULT_NO

public static final String DEFAULT_NO
The Constant DEFAULT_NO.

See Also:
Constant Field Values

PRODUCT_CODE_TYPES

public static final String PRODUCT_CODE_TYPES
The Constant PRODUCT_CODE_TYPES.

See Also:
Constant Field Values

ONE_D_CODE_TYPES

public static final String ONE_D_CODE_TYPES
The Constant ONE_D_CODE_TYPES.

See Also:
Constant Field Values

QR_CODE_TYPES

public static final String QR_CODE_TYPES
The Constant QR_CODE_TYPES.

See Also:
Constant Field Values

ALL_CODE_TYPES

public static final String ALL_CODE_TYPES
The Constant ALL_CODE_TYPES.

Method Detail

initiateScan

public static AlertDialog initiateScan(Activity activity)
See.

Parameters:
activity - the activity
Returns:
the alert dialog initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but uses default English labels.

initiateScan

public static AlertDialog initiateScan(Activity activity,
                                       int stringTitle,
                                       int stringMessage,
                                       int stringButtonYes,
                                       int stringButtonNo)
See.

Parameters:
activity - the activity
stringTitle - the string title
stringMessage - the string message
stringButtonYes - the string button yes
stringButtonNo - the string button no
Returns:
the alert dialog initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but takes string IDs which refer to the Activity 's resource bundle entries.

initiateScan

public static AlertDialog initiateScan(Activity activity,
                                       CharSequence stringTitle,
                                       CharSequence stringMessage,
                                       CharSequence stringButtonYes,
                                       CharSequence stringButtonNo)
See.

Parameters:
activity - the activity
stringTitle - title of dialog prompting user to download Barcode Scanner
stringMessage - text of dialog prompting user to download Barcode Scanner
stringButtonYes - text of button user clicks when agreeing to download Barcode Scanner (e.g. "Yes")
stringButtonNo - text of button user clicks when declining to download Barcode Scanner (e.g. "No")
Returns:
an AlertDialog if the user was prompted to download the app, null otherwise initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but scans for all supported barcode types.

initiateScan

public static AlertDialog initiateScan(Activity activity,
                                       CharSequence stringTitle,
                                       CharSequence stringMessage,
                                       CharSequence stringButtonYes,
                                       CharSequence stringButtonNo,
                                       CharSequence stringDesiredBarcodeFormats)
Invokes scanning.

Parameters:
activity - the activity
stringTitle - title of dialog prompting user to download Barcode Scanner
stringMessage - text of dialog prompting user to download Barcode Scanner
stringButtonYes - text of button user clicks when agreeing to download Barcode Scanner (e.g. "Yes")
stringButtonNo - text of button user clicks when declining to download Barcode Scanner (e.g. "No")
stringDesiredBarcodeFormats - a comma separated list of codes you would like to scan for.
Returns:
an AlertDialog if the user was prompted to download the app, null otherwise

parseActivityResult

public static IntentResult parseActivityResult(int requestCode,
                                               int resultCode,
                                               Intent intent)

Call this from your Activity's.

Parameters:
requestCode - the request code
resultCode - the result code
intent - the intent
Returns:
null if the event handled here was not related to this class, or else an IntentResult containing the result of the scan. If the user cancelled scanning, the fields will be null. Activity.onActivityResult(int, int, Intent) method.


shareText

public static void shareText(Activity activity,
                             CharSequence text)
See.

Parameters:
activity - the activity
text - the text shareText(Activity, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but uses default English labels.

shareText

public static void shareText(Activity activity,
                             CharSequence text,
                             int stringTitle,
                             int stringMessage,
                             int stringButtonYes,
                             int stringButtonNo)
See.

Parameters:
activity - the activity
text - the text
stringTitle - the string title
stringMessage - the string message
stringButtonYes - the string button yes
stringButtonNo - the string button no shareText(Activity, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but takes string IDs which refer to the Activity's resource bundle entries.

shareText

public static void shareText(Activity activity,
                             CharSequence text,
                             CharSequence stringTitle,
                             CharSequence stringMessage,
                             CharSequence stringButtonYes,
                             CharSequence stringButtonNo)
Shares the given text by encoding it as a barcode, such that another user can scan the text off the screen of the device.

Parameters:
activity - the activity
text - the text string to encode as a barcode
stringTitle - title of dialog prompting user to download Barcode Scanner
stringMessage - text of dialog prompting user to download Barcode Scanner
stringButtonYes - text of button user clicks when agreeing to download Barcode Scanner (e.g. "Yes")
stringButtonNo - text of button user clicks when declining to download Barcode Scanner (e.g. "No")