From 1817bb198b6d40e1f5316998d90cf76d5e0da4eb Mon Sep 17 00:00:00 2001 From: nlv Date: Sat, 1 Apr 2017 16:57:11 +0800 Subject: [PATCH] Redefine the default activity request code for using switch statement. Add a construct function to set customized activity request code. --- .../main/java/example/zxing/MainActivity.java | 18 ++++++++++++++++++ sample/src/main/res/layout/activity_main.xml | 6 ++++++ sample/src/main/res/values/strings.xml | 1 + .../integration/android/IntentIntegrator.java | 18 +++++++++++++++--- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/sample/src/main/java/example/zxing/MainActivity.java b/sample/src/main/java/example/zxing/MainActivity.java index 79f74ec49..b3a0119b1 100644 --- a/sample/src/main/java/example/zxing/MainActivity.java +++ b/sample/src/main/java/example/zxing/MainActivity.java @@ -21,6 +21,8 @@ public class MainActivity extends AppCompatActivity { + public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -31,6 +33,10 @@ public void scanBarcode(View view) { new IntentIntegrator(this).initiateScan(); } + public void scanBarcodeWithCustomizedRequestCode(View view) { + new IntentIntegrator(this, CUSTOMIZED_REQUEST_CODE).initiateScan(); + } + public void scanBarcodeInverted(View view){ IntentIntegrator integrator = new IntentIntegrator(this); integrator.addExtra(Intents.Scan.INVERTED_SCAN, true); @@ -87,6 +93,18 @@ public void tabs(View view) { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); + switch (requestCode) { + case CUSTOMIZED_REQUEST_CODE: { + Toast.makeText(this, "REQUEST_CODE = " + requestCode, Toast.LENGTH_LONG).show(); + break; + } + case IntentIntegrator.REQUEST_CODE: { + Toast.makeText(this, "IntentIntegrator default REQUEST_CODE = " + requestCode, Toast.LENGTH_LONG).show(); + break; + } + default: + break; + } if(result != null) { if(result.getContents() == null) { Log.d("MainActivity", "Cancelled scan"); diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 4fd820f68..4331eee32 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -19,6 +19,12 @@ android:text="@string/scan_barcode" android:onClick="scanBarcode"/> +