com.google.zxing.integration.android
Class IntentIntegrator

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

public final class IntentIntegrator
extends java.lang.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 java.lang.String ALL_CODE_TYPES
           
static java.lang.String DEFAULT_MESSAGE
           
static java.lang.String DEFAULT_NO
           
static java.lang.String DEFAULT_TITLE
           
static java.lang.String DEFAULT_YES
           
static java.lang.String ONE_D_CODE_TYPES
           
static java.lang.reflect.Method PACKAGE_SETTER
           
static java.lang.String PRODUCT_CODE_TYPES
           
static java.lang.String QR_CODE_TYPES
           
static int REQUEST_CODE
           
 
Method Summary
static android.app.AlertDialog initiateScan(android.app.Activity activity)
          See initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but uses default English labels.
static android.app.AlertDialog initiateScan(android.app.Activity activity, java.lang.CharSequence stringTitle, java.lang.CharSequence stringMessage, java.lang.CharSequence stringButtonYes, java.lang.CharSequence stringButtonNo)
          See initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but scans for all supported barcode types.
static android.app.AlertDialog initiateScan(android.app.Activity activity, java.lang.CharSequence stringTitle, java.lang.CharSequence stringMessage, java.lang.CharSequence stringButtonYes, java.lang.CharSequence stringButtonNo, java.lang.CharSequence stringDesiredBarcodeFormats)
          Invokes scanning.
static android.app.AlertDialog initiateScan(android.app.Activity activity, int stringTitle, int stringMessage, int stringButtonYes, int stringButtonNo)
          See initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but takes string IDs which refer to the Activity's resource bundle entries.
static IntentResult parseActivityResult(int requestCode, int resultCode, android.content.Intent intent)
          Call this from your Activity's Activity.onActivityResult(int, int, Intent) method.
static void shareText(android.app.Activity activity, java.lang.CharSequence text)
          See shareText(Activity, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but uses default English labels.
static void shareText(android.app.Activity activity, java.lang.CharSequence text, java.lang.CharSequence stringTitle, java.lang.CharSequence stringMessage, java.lang.CharSequence stringButtonYes, java.lang.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(android.app.Activity activity, java.lang.CharSequence text, int stringTitle, int stringMessage, int stringButtonYes, int stringButtonNo)
          See shareText(Activity, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but takes string IDs which refer to the Activity's resource bundle entries.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REQUEST_CODE

public static final int REQUEST_CODE
See Also:
Constant Field Values

DEFAULT_TITLE

public static final java.lang.String DEFAULT_TITLE
See Also:
Constant Field Values

DEFAULT_MESSAGE

public static final java.lang.String DEFAULT_MESSAGE
See Also:
Constant Field Values

DEFAULT_YES

public static final java.lang.String DEFAULT_YES
See Also:
Constant Field Values

DEFAULT_NO

public static final java.lang.String DEFAULT_NO
See Also:
Constant Field Values

PRODUCT_CODE_TYPES

public static final java.lang.String PRODUCT_CODE_TYPES
See Also:
Constant Field Values

ONE_D_CODE_TYPES

public static final java.lang.String ONE_D_CODE_TYPES
See Also:
Constant Field Values

QR_CODE_TYPES

public static final java.lang.String QR_CODE_TYPES
See Also:
Constant Field Values

ALL_CODE_TYPES

public static final java.lang.String ALL_CODE_TYPES

PACKAGE_SETTER

public static final java.lang.reflect.Method PACKAGE_SETTER
Method Detail

initiateScan

public static android.app.AlertDialog initiateScan(android.app.Activity activity)
See initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but uses default English labels.


initiateScan

public static android.app.AlertDialog initiateScan(android.app.Activity activity,
                                                   int stringTitle,
                                                   int stringMessage,
                                                   int stringButtonYes,
                                                   int stringButtonNo)
See initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but takes string IDs which refer to the Activity's resource bundle entries.


initiateScan

public static android.app.AlertDialog initiateScan(android.app.Activity activity,
                                                   java.lang.CharSequence stringTitle,
                                                   java.lang.CharSequence stringMessage,
                                                   java.lang.CharSequence stringButtonYes,
                                                   java.lang.CharSequence stringButtonNo)
See initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but scans for all supported barcode types.

Parameters:
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

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

Parameters:
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
Throws:
java.lang.InterruptedException - if timeout expires before a scan completes

parseActivityResult

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

Call this from your Activity's Activity.onActivityResult(int, int, Intent) method.

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.

shareText

public static void shareText(android.app.Activity activity,
                             java.lang.CharSequence text)
See shareText(Activity, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence) -- same, but uses default English labels.


shareText

public static void shareText(android.app.Activity activity,
                             java.lang.CharSequence text,
                             int stringTitle,
                             int stringMessage,
                             int stringButtonYes,
                             int stringButtonNo)
See 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(android.app.Activity activity,
                             java.lang.CharSequence text,
                             java.lang.CharSequence stringTitle,
                             java.lang.CharSequence stringMessage,
                             java.lang.CharSequence stringButtonYes,
                             java.lang.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:
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")