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(Collection)
.
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
BarcodeFormat.PDF_417
. 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 |
---|---|
static java.util.Collection<java.lang.String> |
ALL_CODE_TYPES |
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 |
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 |
static java.util.Collection<java.lang.String> |
TARGET_ALL_KNOWN |
static java.util.Collection<java.lang.String> |
TARGET_BARCODE_SCANNER_ONLY |
Constructor and Description |
---|
IntentIntegrator(android.app.Activity activity) |
Modifier and Type | Method and Description |
---|---|
void |
addExtra(java.lang.String key,
java.lang.Object value) |
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() |
android.app.AlertDialog |
initiateScan()
Initiates a scan for all known barcode types.
|
android.app.AlertDialog |
initiateScan(java.util.Collection<java.lang.String> desiredBarcodeFormats)
Initiates a scan only for a certain set of barcode types, given as strings corresponding
to their names in ZXing's
BarcodeFormat class like "UPC_A". |
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.Collection<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.
|
protected void |
startActivityForResult(android.content.Intent intent,
int code)
Start an activity.
This method is defined to allow different methods of activity starting for newer versions of Android and for compatibility library. |
public static final int REQUEST_CODE
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
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.Collection<java.lang.String> TARGET_BARCODE_SCANNER_ONLY
public static final java.util.Collection<java.lang.String> TARGET_ALL_KNOWN
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 void setTargetApplications(java.util.Collection<java.lang.String> targetApplications)
public void setSingleTargetApplication(java.lang.String targetApplication)
public java.util.Map<java.lang.String,?> getMoreExtras()
public void addExtra(java.lang.String key, java.lang.Object value)
public android.app.AlertDialog initiateScan()
public 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.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)
public static IntentResult parseActivityResult(int requestCode, int resultCode, android.content.Intent intent)
Call this from your Activity
's
Activity.onActivityResult(int, int, Intent)
method.
IntentResult
containing the result of the scan. If the user cancelled scanning,
the fields will be null.public android.app.AlertDialog shareText(java.lang.CharSequence text)
public 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 otherwise