Java Question Problem with the onActivityResult

alwaysbusy

Expert
Licensed User
Longtime User
I'm trying to write a library for the Zxing barcode reader. I can be done by just starting the Zxing intent and you receive de code in a onActivityResult.

It does work to the part that te app is started, de barcode is found and returned. However, the onActivityResult is not called.

The reason is probably simple: de intent is returning the result to the ba Activity, not to the ABBarcode activity.

But a crash occurs when the line

B4X:
IntentIntegrator.initiateScan(ba.activity);

is changed to

B4X:
IntentIntegrator.initiateScan(ABBarcode.this);


The error happens on startActivityForResult() in the IntentIntegrator.initiateScan function
Here is the IntentIntegrator.initiateScan function:

B4X:
public static AlertDialog initiateScan(Activity activity,
                                         CharSequence stringTitle,
                                         CharSequence stringMessage,
                                         CharSequence stringButtonYes,
                                         CharSequence stringButtonNo,
                                         CharSequence stringDesiredBarcodeFormats) {
    Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
    intentScan.addCategory(Intent.CATEGORY_DEFAULT);
    //intentScan.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);

    // check which types of codes to scan for
    if (stringDesiredBarcodeFormats != null) {
      // set the desired barcode types
      intentScan.putExtra("SCAN_FORMATS", stringDesiredBarcodeFormats);
    }

    try {
      activity.startActivityForResult(intentScan, REQUEST_CODE);
      return null;
    } catch (ActivityNotFoundException e) {
      return showDownloadDialog(activity, stringTitle, stringMessage, stringButtonYes, stringButtonNo);
    }
  }

And here is the library source code.

B4X:
package com.AB.ABBarcode;

import android.app.Activity;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.ActivityObject;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;


@ShortName("ABBarcode")
@Events(values={"BarcodeFound (barCode as String)", "Canceled()"})
@Version(1.0f)
@ActivityObject()
@Author("Alain Bailleul")
public class ABBarcode extends Activity {
   private BA _ba=null;
   private String _eventName="";
   
   
   /**
    *Starts the barcode scanner. The barcode is returned in the BarcodeFound event or the Canceled event
    *eventName (String): name of the ABBarcode object
    *mode (String): QR_CODE_MODE for QR barcodes, "ANYTHING" for everything
    *Example:<code>
    *Sub Button1_Click
    *   myABBarcode.ABBarcodeGet("myABBarcode", "QR_CODE_MODE")
    *End Sub
    *
    *Sub myABBarcode_BarcodeFound(barCode as String)
    *   msgbox(barCode, "")
    *End Sub
    *
    *Sub myABBarcode_Canceled
    *   msgbox("Canceled","")
    *End Sub
    *</code>
    */
   public void ABGetBarcode(final BA ba, String eventName, String mode) {
      _ba=ba;
      _eventName=eventName;
      Log.i("ABGetBarcode", "start");
      ABBarcodeFound("start");
      IntentIntegrator.initiateScan(ba.activity);
      //IntentIntegrator.initiateScan(ABBarcode.this);
                  
   }
   
   
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
      Log.i("onActivityResult", "requestcode=" + requestCode + ":resultCode=" + resultCode);
      switch(requestCode) {
         case IntentIntegrator.REQUEST_CODE: { 
            if (resultCode != RESULT_CANCELED) {
               IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
               if (scanResult != null) {
                  String upc = scanResult.getContents();
                  ABBarcodeFound(upc);
                 }
            }
            else {
               ABCanceled();               
            }
               
            break;
         }
      }
   }
   
   public void ABBarcodeFound(String barCode){
      Log.i("ABBarcodeFound", barCode);
      final String s = _eventName.toLowerCase(BA.cul) + "_barcodefound";
      Log.i("ABBarcodeFound", s);
      _ba.raiseEvent(this, s, barCode);
   }
   
   public void ABCanceled() {
      Log.i("ABCanceled", "Canceled");
      final String s = _eventName.toLowerCase(BA.cul) + "_canceled";
      Log.i("ABCanceled", s);
      _ba.raiseEvent(this, s);
   }
   
   
}

As always, full source code can be posted if needed.

So close, but no sigar ;-)

Any ideas are welcome
 

agraham

Expert
Licensed User
Longtime User
I suspect before long you may be able to this within Basic4android without needing library code.

However, for the present, without the full source to play with I can't really try this but I suspect that the problem is that your ABBarcode is not a real Activity to the OS as it is not registered as such in the application manifest so this is why you get a crash. I think you are correct that the result is being returned to the BA activity but can't be intercepted as it has no onActivityResult method.

I think this will need a modification to the Basic4android Activity object and I'm pretty sure this is something Erel has already thought out. Custom Intents arrived this morning in the Phone library so I guess onActivityResult might be in the next release.
 
Top