Android Question How To StartService another app and receive result

Willian Padilha

Member
Licensed User
Hi, i have this code and works fine.


B4A StartService Intent:
Dim s_template As String
    s_template = $"\u0010CT~~CD,~CC^~CT~${CRLF}
                ^XA~TA000~JSN^LT0^MNW^MTD^PON^PMN^LH0,0^JMA^PR4,4~SD10^JUS^LRN^CI0^XZ${CRLF}
                 ^XA${CRLF}
                 ^MMT${CRLF}
                 ^PW591${CRLF}
                 ^LL0203${CRLF}
                 ^LS0${CRLF}
                 ^FT171,82^A0N,27,26^FH\\^FD%PRODUCT_NAME%^FS${CRLF}
                 ^FT222,107^A0N,17,16^FH\\^FD%MSRP%^FS${CRLF}
                 ^FT424,163^A0N,23,24^FB82,1,0,R^FH\\^FD%PCT%^FS${CRLF}
                 ^FT314,167^A0N,28,28^FH\\^FD%FINAL%^FS${CRLF}
                 ^FT367,107^A0N,17,16^FH\\^FD%UPC_CODE%^FS${CRLF}
                 ^FT471,138^A0N,14,14^FH\\^FDYou saved:^FS${CRLF}
                 ^FO451,119^GB103,54,2^FS${CRLF}
                 ^FT171,20^A0N,17,16^FH\\^FDPrintConnect Template Print Example^FS${CRLF}
                 ^FT171,167^A0N,28,28^FH\\^FDFinal Price:^FS${CRLF}
                 ^FT171,51^A0N,17,16^FH\\^FDProduct:^FS${CRLF}
                 ^FT171,107^A0N,17,16^FH\\^FDMSRP:^FS${CRLF}
                 ^FT508,163^A0N,23,24^FH\\^FD%^FS${CRLF}
                 ^FT328,107^A0N,17,16^FH\\^FDUPC:^FS${CRLF}
                 ^FO171,119^GB259,0,2^FS${CRLF}
                 ^PQ1,0,1,Y^XZ${CRLF}"$
    Dim bytesArray() As Byte
    Dim m_map_args As Map
    m_map_args.Initialize
    bytesArray = s_template.GetBytes("UTF8")
    m_map_args.put("%PRODUCT_NAME%", "Apples")
    m_map_args.put("%MSRP%", "$1.00")
    m_map_args.put("%PCT%", "50")
    m_map_args.put("%FINAL%", "$0.50")
    m_map_args.put("%UPC_CODE%", "12345678")
   
    Dim intentPrint As Intent  
    intentPrint.Initialize("","")
    intentPrint.SetComponent("com.zebra.printconnect/.print.TemplatePrintWithContentService")
    intentPrint.PutExtra("com.zebra.printconnect.PrintService.TEMPLATE_DATA",bytesArray)
    intentPrint.PutExtra("com.zebra.printconnect.PrintService.VARIABLE_DATA",m_map_args)
    StartService(intentPrint)


But in the Guide of the another APP (Zebra PrintConnect), return status of process with buildIPCSafeReceiver.
How to write this code in B4A (buildIPCSafeReceiver) ?

I need the return to notify the user in case of an error


Link of User Guide Zebra PrintConnect (page 88 - Intents)



Zebra PrintConnect Guide:
Button templatePrint = (Button) findViewById(R.id.templatePrint);
templatePrint.setOnClickListener(new View.OnClickListener() {
[USER=69643]@override[/USER]
public void onClick(View v) {
// Define a hash map of variable data
// Strings used for keys will be replaced by their corresponding values in your template file's ZPL
HashMap<String, String> variableData = new HashMap<>();
variableData.put("%PRODUCT_NAME%", "Apples");
variableData.put("%MSRP%", "$1.00");
variableData.put("%PCT%", "50");
variableData.put("%FINAL%", "$0.50");
variableData.put("%UPC_CODE%", "12345678");
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.zebra.printconnect","com.zebra.printconnect.print.TemplatePrintService"));
intent.putExtra("com.zebra.printconnect.PrintService.TEMPLATE_FILE_NAME", "PriceTagTemplate.zpl");
intent.putExtra("com.zebra.printconnect.PrintService.VARIABLE_DATA", variableData);
intent.putExtra("com.zebra.printconnect.PrintService.RESULT_RECEIVER", buildIPCSafeReceiver(new ResultReceiver(null) {
[USER=69643]@override[/USER]
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (resultCode == 0) { // Result code 0 indicates success
// Handle successful print
} else {
// Handle unsuccessful print
// Error message (null on successful print)
String errorMessage = resultData.getString("com.zebra.printconnect.PrintService.ERROR_MESSAGE");
}
}
}));
startService(intent);
}
});

This is a method buildIPCSafeReceiver

buildIPCSafeReceiver:
// This method makes your ResultReceiver safe for inter-process communication
private ResultReceiver buildIPCSafeReceiver(ResultReceiver actualReceiver) {
 Parcel parcel = Parcel.obtain();
 actualReceiver.writeToParcel(parcel, 0);
 parcel.setDataPosition(0);
 ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel);
 parcel.recycle();
 return receiverForSending;
}

PS: StartActivity doesn't work for this situation.

best regards
 
Last edited:
Top