Java Question onActivityResult

alwaysbusy

Expert
Licensed User
Longtime User
Last edited:

alwaysbusy

Expert
Licensed User
Longtime User
thanks for the tutorial Erel!

However I seem to have two problems:

1. ion = new IOnActivityResult(); gives an error
2. the ba.startActivityForResult() misses one parameter I think. A normal startActivityForResult is with the params Intent, int so I would suspect your new startActivityForResult would need IOnActivityResult, Intent, Int.

Here is the full code:

B4X:
package com.AB.ABZxing;

import android.app.Activity;
import android.app.AlertDialog;
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 android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import anywheresoftware.b4a.IOnActivityResult;

@ShortName("ABBarcode")
@Events(values={"BarcodeFound (barCode as String, formatName as string)", "Canceled()"})
@Version(1.0f)
@Author("Alain Bailleul")
public class ABZxing implements IOnActivityResult {
   private BA _ba=null;
   private String _eventName="";
   private static IOnActivityResult ion;
   
   private static final String DEFAULT_TITLE = "Install Barcode Scanner?";
   private static final String DEFAULT_MESSAGE = "This application requires Barcode Scanner. Would you like to install it?";
   private static final String DEFAULT_YES = "Yes";
   private static final String DEFAULT_NO = "No";
   private static final int REQUEST_CODE = 0x0ba7c0de;
      
   public void ABGetBarcode(final BA ba, String eventName, String mode) {
       _ba=ba;
       _eventName=eventName;
      
       Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
       intentScan.addCategory(Intent.CATEGORY_DEFAULT);
       
       if (mode != null) {
         intentScan.putExtra("SCAN_FORMATS", mode);
       }
           
      [B][COLOR="Red"] ion = new IOnActivityResult();[/COLOR][/B]
       
       try {
         [COLOR="SeaGreen"][B]// needs to be: ba.startActivityForResult(ion, intentScan, REQUEST_CODE);
          // to start the scanner and requesting an answer to ResultArrived[/B][/COLOR]
          ba.startActivityForResult(ion, intentScan);
       } catch (ActivityNotFoundException e) {
           showDownloadDialog(ba.activity, DEFAULT_TITLE, DEFAULT_MESSAGE, DEFAULT_YES, DEFAULT_NO);
       }
   }
   
   @SuppressWarnings("unchecked")
        @Override
        public void ResultArrived(int resultCode, Intent intent) {
      if (resultCode != Activity.RESULT_CANCELED) {
          if (resultCode == Activity.RESULT_OK) {
              String contents = intent.getStringExtra("SCAN_RESULT");
              String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
              final String s = _eventName.toLowerCase(BA.cul) + "_barcodefound";
            _ba.raiseEvent(this, s, contents, formatName);
          } else {
             final String s = _eventName.toLowerCase(BA.cul) + "_barcodefound";
             _ba.raiseEvent(this, s, "", "");
          }
      }
      else {
         final String s = _eventName.toLowerCase(BA.cul) + "_canceled";
         _ba.raiseEvent(this, s);               
      }
   }
   
   private static AlertDialog showDownloadDialog(final Activity activity,
            CharSequence stringTitle,
            CharSequence stringMessage,
            CharSequence stringButtonYes,
            CharSequence stringButtonNo) {
      AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);
      downloadDialog.setTitle(stringTitle);
      downloadDialog.setMessage(stringMessage);
      downloadDialog.setPositiveButton(stringButtonYes, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialogInterface, int i) {
         Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
         activity.startActivity(intent);
      }
      });
      downloadDialog.setNegativeButton(stringButtonNo, new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialogInterface, int i) {}
      });
      return downloadDialog.show();
   }

   
}
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. IOnActivityResult is an interface. You cannot instantiate it directly.
You need something like:
B4X:
ion = new IOnActivityResult() {

                @Override
                public void ResultArrived(int resultCode, Intent intent) {
                    List list = new List();
                    if (resultCode == Activity.RESULT_OK) {
                        ArrayList<String> t = intent.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                        if (t.size() > 0) {
                            list.setObject((java.util.ArrayList)t);
                        }
                    }
                    ion = null;
                    ba.raiseEvent(VoiceRecognition.this, eventName + "_result", list.IsInitialized(), list);

                }
            };
This code actually creates an anonymous class which implements the interface.

2. The last int is not required as Basic4android takes care of matching the requests to the IOnActivityResult object.

I've moved this thread to the developers sub-forum.
 
Top