Re: [Android] startActivityForResult & Callback

2,432 views
Skip to first unread message

Txxoo

unread,
Nov 5, 2012, 4:39:20 AM11/5/12
to phon...@googlegroups.com
I try to write a lighter barcodescanner without whole ZXing code but just the integrator, here is my code. But the onActivityResult is not called. Do you know how can I do that, please ?

public class BarcodeScanner extends Plugin {

    /**
     * Executes the request and returns PluginResult.
     *
     * @param action        The action to execute.
     * @param args          JSONArray of arguments for the plugin.
     * @param callbackId    The callback id used when calling back into JavaScript.
     * @return              A PluginResult object with a status and message.
     */
    public PluginResult execute(String action, JSONArray args, String callbackId) {
        this.callback = callbackId;

        if (action.equals(SCAN)) {
            scan();
        } else {
            return new PluginResult(PluginResult.Status.INVALID_ACTION);
        }
        PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
        r.setKeepCallback(true);
        return r;
    }


    /**
     * Starts an intent to scan and decode a barcode.
     */
    public void scan() {
       


       
        Runnable runnable = new Runnable() {
            public void run() {
                cordova.setActivityResultCallback(BarcodeScanner.this);
                IntentIntegrator integrator = new IntentIntegrator(cordova.getActivity());
            }

       
        };

        this.cordova.getActivity().runOnUiThread(runnable);       
       
    }

    /**
     * Called when the barcode scanner intent completes
     *
     * @param requestCode       The request code originally supplied to startActivityForResult(),
     *                          allowing you to identify who this result came from.
     * @param resultCode        The integer result code returned by the child activity through its setResult().
     * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
       
       
        if (requestCode == REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                JSONObject obj = new JSONObject();
                try {
                    obj.put(TEXT, intent.getStringExtra("SCAN_RESULT"));
                    obj.put(FORMAT, intent.getStringExtra("SCAN_RESULT_FORMAT"));
                    obj.put(CANCELLED, false);
                } catch(JSONException e) {
                    //Log.d(LOG_TAG, "This should never happen");
                }
                this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
            } if (resultCode == Activity.RESULT_CANCELED) {
                JSONObject obj = new JSONObject();
                try {
                    obj.put(TEXT, "");
                    obj.put(FORMAT, "");
                    obj.put(CANCELLED, true);
                } catch(JSONException e) {
                    //Log.d(LOG_TAG, "This should never happen");
                }
                this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
            } else {
                this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
            }
        }
    }


}

Sean Fair

unread,
Jan 8, 2013, 11:40:06 AM1/8/13
to phon...@googlegroups.com
Txxoo <tx.youtube.20.txxoo@...> writes:

>
>
> I try to write a lighter barcodescanner without whole ZXing code but
> just the integrator, here is my code. But the onActivityResult is not
> called. Do you know how can I do that, please ?public class BarcodeScanner
extends Plugin {    /**     * Executes the request and returns PluginResult.    
*     * <at> param action        The action to execute.     * <at> param
args          JSONArray of arguments for the plugin.     * <at> param
callbackId    The callback id used when calling back into JavaScript.     *
<at> return              A PluginResult object with a status and message.    
*/    public PluginResult execute(String action, JSONArray args, String
callbackId) {        this.callback = callbackId;        if (action.equals(SCAN))
{            scan();        } else {            return new
PluginResult(PluginResult.Status.INVALID_ACTION);        }        PluginResult r
= new PluginResult(PluginResult.Status.NO_RESULT);       
r.setKeepCallback(true);        return r;    }    /**     * Starts an intent to
scan and decode a barcode.     */    public void scan() {                       
Runnable runnable = new Runnable() {            public void run() {           
    cordova.setActivityResultCallback(BarcodeScanner.this);               
IntentIntegrator integrator = new IntentIntegrator(cordova.getActivity());   
        }                };       
this.cordova.getActivity().runOnUiThread(runnable);                    }   
/**     * Called when the barcode scanner intent completes     *     * <at>
param requestCode       The request code originally supplied to
startActivityForResult(),     *                          allowing you to
identify who this result came from.     * <at> param resultCode        The
integer result code returned by the child activity through its setResult().    
> * <at> param intent            An Intent, which can return result data to
> the caller (various data can be attached to Intent "extras").     */    <at>
Override    public void onActivityResult(int requestCode, int resultCode, Intent
intent) {                        if (requestCode == REQUEST_CODE) {           
if (resultCode == Activity.RESULT_OK) {                JSONObject obj = new
JSONObject();                try {                    obj.put(TEXT,
intent.getStringExtra("SCAN_RESULT"));                    obj.put(FORMAT,
intent.getStringExtra("SCAN_RESULT_FORMAT"));                   
obj.put(CANCELLED, false);                } catch(JSONException e)
{                    //Log.d(LOG_TAG, "This should never
happen");                }                this.success(new
PluginResult(PluginResult.Status.OK, obj), this.callback);            } if
(resultCode == Activity.RESULT_CANCELED) {                JSONObject obj = new
JSONObject();                try {                    obj.put(TEXT,
"");                    obj.put(FORMAT, "");                   
obj.put(CANCELLED, true);                } catch(JSONException e)
{                    //Log.d(LOG_TAG, "This should never
happen");                }                this.success(new
PluginResult(PluginResult.Status.OK, obj), this.callback);            } else
{                this.error(new PluginResult(PluginResult.Status.ERROR),
this.callback);            }        }    }}
>
>


Saw your answer for your problem.. but some of the Native Android guys who need
to hook into PhoneGap may want to use a thread to start an Activity. The way you
can get your Activity to come back to your plug in is to make sure that :

this.cordova.setActivityResultCallback(MyActivity.this);

is called before you do the startActivityForResult()... call. Something that
really was bothering me that no one seemed to have answered anywhere.

Sean Fair

Reply all
Reply to author
Forward
0 new messages