我正在使用C ++ Builder 10.1 Berlin来开发一个扫描条形码的Android应用程序。要做到这一点,当用户点击我的应用程序中的按钮时,我正在调用另一个应用程序(Zxing),如下所示:
_di_JIntent intent;
if(Global->ClipService != NULL){
Global->ClipService->SetClipboard(TValue::_op_Implicit(NULL));
intent = TJIntent::Create();
intent->setAction(StringToJString("com.google.zxing.client.android.SCAN"));
SharedActivity()->startActivityForResult(intent,0);
scanCalled = true;
}
else{
ShowMessage("Presse Papier non disponible!");
}
要获取读取条形码,我使用剪贴板服务。到现在为止,这对我来说已经足够了。
现在,我想获得条形码的类型。我需要处理Android的onActivityResult
事件。我在Delphi中找到了一个解决方案,但是我无法将其转换为C ++:
Launching activities and handling results in Delphi XE6 Android apps
特别是标题为"Communication from the launched activity"。
的部分实际上,函数TMessageManager.DefaultManager.SubscribeToMessage()
在C ++中需要TMessageListener
,但我不知道如何使用它。
答案 0 :(得分:2)
所以,真正的问题只是你不明白如何在C ++中调用TMessageManager::SubscribeToMessage()
。您应该首先阅读包含C ++示例的文档。
Sending and Receiving Messages Using the RTL
Brian Long's Delphi example将在C ++中转换为以下内容:
#include <System.Messaging.hpp>
//...
class TMainForm : public TForm
{
//...
private:
static const int ScanRequestCode = 0;
int FMessageSubscriptionID;
void __fastcall HandleActivityMessage(TObject* const Sender, TMessageBase* const M);
bool __fastcall OnActivityResult(int RequestCode, int ResultCode, _di_JIntent Data);
//...
};
#include <FMX.Platform.Android.hpp>
#include <Androidapi.Helpers.hpp>
#include <Androidapi.JNI.App.hpp>
#include <Androidapi.JNI.Toast.hpp>
#include <LaunchActivities.hpp>
// ...
void __fastcall TMainForm::BarcodeScannerButtonClick(TObject *Sender)
{
FMessageSubscriptionID = TMessageManager::DefaultManager->SubscribeToMessage(__classid(TMessageResultNotification), &HandleActivityMessage);
LaunchQRScanner(ScanRequestCode);
}
void __fastcall TMainForm::HandleActivityMessage(TObject* const Sender, TMessageBase* const M)
{
TMessageResultNotification *msg = dynamic_cast<TMessageResultNotification*>(M);
if (msg)
OnActivityResult(msg->RequestCode, msg->ResultCode, msg->Value);
}
bool __fastcall TMainForm::OnActivityResult(int RequestCode, int ResultCode, _di_JIntent Data)
{
String ScanContent, ScanFormat;
TMessageManager::DefaultManager->Unsubscribe(__classid(TMessageResultNotification), FMessageSubscriptionID);
FMessageSubscriptionID = 0;
// For more info see https://github.com/zxing/zxing/wiki/Scanning-Via-Intent
if (RequestCode == ScanRequestCode)
{
if (ResultCode == TJActivity::JavaClass->RESULT_OK)
{
if (Data)
{
ScanContent = JStringToString(Data->getStringExtra(StringToJString("SCAN_RESULT")));
ScanFormat = JStringToString(Data.getStringExtra(StringToJString("SCAN_RESULT_FORMAT")));
Toast(Format("Found %s format barcode:\n%s", ARRAYOFCONST(( ScanFormat, ScanContent ))), LongToast);
}
}
else if (ResultCode == TJActivity::JavaClass->RESULT_CANCELED)
{
Toast("You cancelled the scan", ShortToast);
}
return true;
}
return false;
}