public class IntentIntegrator
extends java.lang.Object
A utility class which helps ease integration with Barcode Scanner via Intent
s. 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.
To integrate, create an instance of IntentIntegrator
and call initiateScan()
and wait
for the result in your app.
It does require that the Barcode Scanner (or work-alike) application is installed. The
initiateScan()
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 integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();
Note that initiateScan()
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.
You can use setTitle(String)
to customize the title of this download prompt dialog (or, use
setTitleByID(int)
to set the title by string resource ID.) Likewise, the prompt message, and
yes/no button labels can be changed.
Finally, you can use addExtra(String, Object)
to add more parameters to the Intent used
to invoke the scanner. This can be used to set additional options not directly exposed by this
simplified API.
By default, this will only allow applications that are known to respond to this intent correctly
do so. The apps that are allowed to response can be set with setTargetApplications(List)
.
For example, set to TARGET_BARCODE_SCANNER_ONLY
to only target the Barcode Scanner app itself.
To share text, encoded as a QR Code on-screen, similarly, see shareText(CharSequence)
.
Some code, particularly download integration, was contributed from the Anobiit application.
Some formats are not enabled by default even when scanning with ALL_CODE_TYPES
, such as
PDF417. Use initiateScan(java.util.Collection)
with
a collection containing the names of formats to scan for explicitly, like "PDF_417", to use such
formats.
Modifier and Type | Field and Description |
---|---|
private android.app.Activity |
activity |
static java.util.Collection<java.lang.String> |
ALL_CODE_TYPES |
private static java.lang.String |
BS_PACKAGE |
private static java.lang.String |
BSPLUS_PACKAGE |
private java.lang.String |
buttonNo |
private java.lang.String |
buttonYes |
static java.util.Collection<java.lang.String> |
DATA_MATRIX_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 |
private android.app.Fragment |
fragment |
private java.lang.String |
message |
private java.util.Map<java.lang.String,java.lang.Object> |
moreExtras |
static java.util.Collection<java.lang.String> |
ONE_D_CODE_TYPES |
static java.util.Collection<java.lang.String> |
PRODUCT_CODE_TYPES |
static java.util.Collection<java.lang.String> |
QR_CODE_TYPES |
static int |
REQUEST_CODE |
private static java.lang.String |
TAG |
static java.util.List<java.lang.String> |
TARGET_ALL_KNOWN |
static java.util.List<java.lang.String> |
TARGET_BARCODE_SCANNER_ONLY |
private java.util.List<java.lang.String> |
targetApplications |
private java.lang.String |
title |
Constructor and Description |
---|
IntentIntegrator(android.app.Activity activity) |
IntentIntegrator(android.app.Fragment fragment) |
Modifier and Type | Method and Description |
---|---|
void |
addExtra(java.lang.String key,
java.lang.Object value) |
private void |
attachMoreExtras(android.content.Intent intent) |
private static boolean |
contains(java.lang.Iterable<android.content.pm.ResolveInfo> availableApps,
java.lang.String targetApp) |
private java.lang.String |
findTargetAppPackage(android.content.Intent intent) |
java.lang.String |
getButtonNo() |
java.lang.String |
getButtonYes() |
java.lang.String |
getMessage() |
java.util.Map<java.lang.String,?> |
getMoreExtras() |
java.util.Collection<java.lang.String> |
getTargetApplications() |
java.lang.String |
getTitle() |
private void |
initializeConfiguration() |
android.app.AlertDialog |
initiateScan()
Initiates a scan for all known barcode types with the default camera.
|
android.app.AlertDialog |
initiateScan(java.util.Collection<java.lang.String> desiredBarcodeFormats)
Initiates a scan, using the default camera, only for a certain set of barcode types, given as strings corresponding
to their names in ZXing's
BarcodeFormat class like "UPC_A". |
android.app.AlertDialog |
initiateScan(java.util.Collection<java.lang.String> desiredBarcodeFormats,
int cameraId)
Initiates a scan, using the specified camera, only for a certain set of barcode types, given as strings corresponding
to their names in ZXing's
BarcodeFormat class like "UPC_A". |
android.app.AlertDialog |
initiateScan(int cameraId)
Initiates a scan for all known barcode types with the specified camera.
|
private static java.util.List<java.lang.String> |
list(java.lang.String... values) |
static IntentResult |
parseActivityResult(int requestCode,
int resultCode,
android.content.Intent intent)
Call this from your
Activity 's
Activity.onActivityResult(int, int, Intent) method. |
void |
setButtonNo(java.lang.String buttonNo) |
void |
setButtonNoByID(int buttonNoID) |
void |
setButtonYes(java.lang.String buttonYes) |
void |
setButtonYesByID(int buttonYesID) |
void |
setMessage(java.lang.String message) |
void |
setMessageByID(int messageID) |
void |
setSingleTargetApplication(java.lang.String targetApplication) |
void |
setTargetApplications(java.util.List<java.lang.String> targetApplications) |
void |
setTitle(java.lang.String title) |
void |
setTitleByID(int titleID) |
android.app.AlertDialog |
shareText(java.lang.CharSequence text)
Defaults to type "TEXT_TYPE".
|
android.app.AlertDialog |
shareText(java.lang.CharSequence text,
java.lang.CharSequence type)
Shares the given text by encoding it as a barcode, such that another user can
scan the text off the screen of the device.
|
private android.app.AlertDialog |
showDownloadDialog() |
protected void |
startActivityForResult(android.content.Intent intent,
int code)
Start an activity.
|
public static final int REQUEST_CODE
private static final java.lang.String TAG
public static final java.lang.String DEFAULT_TITLE
public static final java.lang.String DEFAULT_MESSAGE
public static final java.lang.String DEFAULT_YES
public static final java.lang.String DEFAULT_NO
private static final java.lang.String BS_PACKAGE
private static final java.lang.String BSPLUS_PACKAGE
public static final java.util.Collection<java.lang.String> PRODUCT_CODE_TYPES
public static final java.util.Collection<java.lang.String> ONE_D_CODE_TYPES
public static final java.util.Collection<java.lang.String> QR_CODE_TYPES
public static final java.util.Collection<java.lang.String> DATA_MATRIX_TYPES
public static final java.util.Collection<java.lang.String> ALL_CODE_TYPES
public static final java.util.List<java.lang.String> TARGET_BARCODE_SCANNER_ONLY
public static final java.util.List<java.lang.String> TARGET_ALL_KNOWN
private final android.app.Activity activity
private final android.app.Fragment fragment
private java.lang.String title
private java.lang.String message
private java.lang.String buttonYes
private java.lang.String buttonNo
private java.util.List<java.lang.String> targetApplications
private final java.util.Map<java.lang.String,java.lang.Object> moreExtras
public IntentIntegrator(android.app.Activity activity)
activity
- Activity
invoking the integrationpublic IntentIntegrator(android.app.Fragment fragment)
fragment
- Fragment
invoking the integration.
startActivityForResult(Intent, int)
will be called on the Fragment
instead
of an Activity
private void initializeConfiguration()
public java.lang.String getTitle()
public void setTitle(java.lang.String title)
public void setTitleByID(int titleID)
public java.lang.String getMessage()
public void setMessage(java.lang.String message)
public void setMessageByID(int messageID)
public java.lang.String getButtonYes()
public void setButtonYes(java.lang.String buttonYes)
public void setButtonYesByID(int buttonYesID)
public java.lang.String getButtonNo()
public void setButtonNo(java.lang.String buttonNo)
public void setButtonNoByID(int buttonNoID)
public java.util.Collection<java.lang.String> getTargetApplications()
public final void setTargetApplications(java.util.List<java.lang.String> targetApplications)
public void setSingleTargetApplication(java.lang.String targetApplication)
public java.util.Map<java.lang.String,?> getMoreExtras()
public final void addExtra(java.lang.String key, java.lang.Object value)
public final android.app.AlertDialog initiateScan()
AlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwise.public final android.app.AlertDialog initiateScan(int cameraId)
cameraId
- camera ID of the camera to use. A negative value means "no preference".AlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwise.public final android.app.AlertDialog initiateScan(java.util.Collection<java.lang.String> desiredBarcodeFormats)
BarcodeFormat
class like "UPC_A". You can supply constants
like PRODUCT_CODE_TYPES
for example.desiredBarcodeFormats
- names of BarcodeFormat
s to scan forAlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwise.public final android.app.AlertDialog initiateScan(java.util.Collection<java.lang.String> desiredBarcodeFormats, int cameraId)
BarcodeFormat
class like "UPC_A". You can supply constants
like PRODUCT_CODE_TYPES
for example.desiredBarcodeFormats
- names of BarcodeFormat
s to scan forcameraId
- camera ID of the camera to use. A negative value means "no preference".AlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwiseprotected void startActivityForResult(android.content.Intent intent, int code)
intent
- Intent to start.code
- Request code for the activityActivity.startActivityForResult(Intent, int)
,
Fragment.startActivityForResult(Intent, int)
private java.lang.String findTargetAppPackage(android.content.Intent intent)
private static boolean contains(java.lang.Iterable<android.content.pm.ResolveInfo> availableApps, java.lang.String targetApp)
private android.app.AlertDialog showDownloadDialog()
public static IntentResult parseActivityResult(int requestCode, int resultCode, android.content.Intent intent)
Call this from your Activity
's
Activity.onActivityResult(int, int, Intent)
method.
requestCode
- request code from onActivityResult()
resultCode
- result code from onActivityResult()
intent
- Intent
from onActivityResult()
IntentResult
containing the result of the scan. If the user cancelled scanning,
the fields will be null.public final android.app.AlertDialog shareText(java.lang.CharSequence text)
text
- the text string to encode as a barcodeAlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwiseshareText(CharSequence, CharSequence)
public final android.app.AlertDialog shareText(java.lang.CharSequence text, java.lang.CharSequence type)
text
- the text string to encode as a barcodetype
- type of data to encode. See com.google.zxing.client.android.Contents.Type
constants.AlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwiseprivate static java.util.List<java.lang.String> list(java.lang.String... values)
private void attachMoreExtras(android.content.Intent intent)