我正在 C++ Builder 中在 android 上生成一个应用程序,它安装一个 APK 并重新启动已安装的应用程序。我可以在我们的平板电脑(货运应用程序)上单独完成这两项操作。但是,由于安装需要安装用户确认,这可能需要一段时间。问题是我希望在开始应用程序之前完成意图并获得结果。
我从来没有离开这里的 While 循环。下面是调用 apk 安装意图的主要代码和允许我退出 while 循环的方法“onActivityResult”。我已经看到将覆盖 onActivityResult 的 Delphi 代码,但我似乎无法让 C++ Builder 允许我覆盖成员函数。请原谅多个注释掉的代码,它们是我尝试过但也没有用的方法。我也留下了 messageManager 代码,但是消息处理程序也从未被调用过。
在此方面的任何帮助将不胜感激。
void __fastcall TForm5::Button3Tap(TObject *Sender, const TPointF &Point)
{
//GetDirs(); Works, so comment out for now to concentrate on intent function
UnicodeString filePath =
"/storage/emulated/0/Android/data/com.embarcadero.Immixt/files/Download/Immixt.apk";
if (TFile::Exists(filePath)) {
_di_JContext Context = TAndroidHelper::Context;
// Create the intent to install the APK file
// Set the path to the APK file in the app's data directory
_di_JString ApkFilePath = StringToJString(filePath);
UnicodeString mimeType = "application/vnd.android.package-archive";
_di_JString mimeType1 = StringToJString(mimeType);
// Create the intent to install the APK file
intent = TJIntent::JavaClass->init();
intent->setAction(TJIntent::JavaClass->ACTION_INSTALL_PACKAGE);
//intent->setAction(TJIntent::JavaClass->ACTION_INSTALL_PACKAGE);
intent->addFlags(TJIntent::JavaClass->FLAG_ACTIVITY_NEW_TASK);
intent->addFlags(TJIntent::JavaClass->FLAG_GRANT_READ_URI_PERMISSION);
intent->addFlags(TJIntent::JavaClass->FLAG_GRANT_WRITE_URI_PERMISSION);
intent->putExtra(TJIntent::JavaClass->EXTRA_NOT_UNKNOWN_SOURCE, true);
intent->putExtra(TJIntent::JavaClass->EXTRA_RETURN_RESULT, true);
_di_Jnet_Uri Uri1;
_di_JFile APKFile = TJFile::JavaClass->init(ApkFilePath);
try {
Uri1 = TAndroidHelper::JFileToJURI(APKFile);
} catch (const Exception& e) {
//CLog::d(TAG, e);
_di_Jnet_Uri Uri1 = TJnet_Uri::JavaClass->parse(ApkFilePath);
}
//_di_Jnet_Uri Uri1 = TJnet_Uri::JavaClass->parse(ApkFilePath);
intent->setDataAndType(Uri1, mimeType1);
// Start the activity to install the APK file
activityResult = -1;
//Context->startActivity(intent);
FMessageSubscriptionID = TMessageManager::DefaultManager-
>SubscribeToMessage(__classid(TMessageResultNotification), HandleActivityMessage);
//TAndroidHelper::OnActivityResult = &onActivityResult;
//TAndroidHelper::Activity->startActivityForResult(intent, 0);
SharedActivity()->startActivityForResult(intent, 1234);
/*
TAndroidHelper::Activity->startActivityForResult(intent, 1234,
[this](int requestCode, int resultCode, _di_JIntent data)
{
Form5->onActivityResult(requestCode, resultCode, data);
});
*/
while (activityResult == -1)
{
Sleep(100);
}
// Wait for the activity to complete
}
/---------------------------------------------------------------------------------------
void onActivityResult(int i, int i1, Androidapi::Jni::Graphicscontentviewtext::_di_JIntent
intent)
{
TMessageManager::DefaultManager->Unsubscribe(__classid(TMessageResultNotification),
Form5->FMessageSubscriptionID);
Form5->FMessageSubscriptionID = 0;
// For more info see https://github.com/zxing/zxing/wiki/Scanning-Via-Intent
if (i == 1234)
{
if (i1 == TJActivity::JavaClass->RESULT_OK)
{
Form5->activityResult =0;
}
else if (i1 == TJActivity::JavaClass->RESULT_CANCELED)
{
Form5->activityResult =0;
}
}
}
我期待下面的代码等待,直到 activityResult 被更改为退出 while 循环。 activityResult 在 onActivityResult 中被修改,根据所有文档,它应该由 SharedActivity()->startActivityForResult(intent, 1234) 调用。但它从来没有