我正在尝试将 Java 代码转换为 Delphi 代码,以便使用 Android 应用程序(DataWedge 应用程序)的 API。
为了结合上下文,DataWegde 还允许将 Zebra 终端的激光阅读器读取的条形码发送到 Android 应用程序。为此,需要创建专用于应用程序的配置文件,以指示应用程序如何接收条形码。特别是,此配置文件必须包含该配置文件适用的应用程序列表。
下面的 Java 代码来自 DataWedge API 文档 (techdocs.zebra.com/datawedge/13-0/guide/api/setconfig/)
// CREATE APP_LIST BUNDLES (apps and/or activities to be associated with the Profile)
Bundle bundleApp1 = new Bundle();
bundleApp1.putString("PACKAGE_NAME","com.symbol.emdk.simulscansample1");
bundleApp1.putStringArray("ACTIVITY_LIST", new String[]{
"com.symbol.emdk.simulscansample1.DeviceControl",
"com.symbol.emdk.simulscansample1.MainActivity",
"com.symbol.emdk.simulscansample1.ResultsActivity.*",
"com.symbol.emdk.simulscansample1.ResultsActivity2",
"com.symbol.emdk.simulscansample1.SettingsFragment1"});
Bundle bundleApp2 = new Bundle();
bundleApp2.putString("PACKAGE_NAME","com.example.intents.datawedgeintent");
bundleApp2.putStringArray("ACTIVITY_LIST", new String[]{
"com.example.intents.datawedgeintent.DeviceControl",
"com.example.intents.datawedgeintent.MainActivity",
"com.example.intents.datawedgeintent.ResultsActivity",
"com.example.intents.datawedgeintent.SettingsFragment1"});
// NEXT APP_LIST BUNDLE(S) INTO THE MAIN BUNDLE
bMain.putParcelableArray("APP_LIST", new Bundle[]{
bundleApp1
,bundleApp2
});
Intent i = new Intent();
i.setAction("com.symbol.datawedge.api.ACTION");
i.putExtra("com.symbol.datawedge.api.SET_CONFIG", bMain);
this.sendBroadcast(i);
我对这部分代码有困难:
bMain.putParcelableArray("APP_LIST", new Bundle[]{
bundleApp1
,bundleApp2
});
我对 Java 不熟悉,但是“new Bundle[]”创建了一个包数组,并且该数组使用“ParcelableArray”放入 bMain 包中,这似乎是一种不同的数据类型。
使用 Delphi,我只能通过显式转换变量来做到这一点。所以我编写了以下代码(更简单的代码,因为我只有一个应用程序,并且 API 允许通配符“*”作为活动列表):
// Variables declarations
var activitiesList : TJavaObjectArray<JString>;
iSetConfig: JIntent;
bMain : JBundle;
bApp : JBundle;
bAppList : TJavaObjectArray<JBundle>;
// Also tried that
// bAppList : TJavaObjectArray<JParcelable>;
begin
// Application
activitiesList := TJavaObjectArray<JString>.Create(1);
// "j" function converts Delphi string to Java String
// like this jStr := TAndroidHelper.StringToJString(dStr)
// just making the code more readable
activitiesList.Items[0] := j('*');
bApp := TJBundle.Create;
bApp.putString(j('PACKAGE_NAME'),j('my.application.test'));
bApp.putStringArray(j('ACTIVITY_LIST'), activitiesList);
bAppList := TJavaObjectArray<JBundle>.create(1);
// Also tried that
// bAppList := TJavaObjectArray<JParcelable>.create(1);
bAppList.Items[0] := bApp;
// Also tried that
// bAppList.Items[0] := JParcelable(bApp);
bMain := TJBundle.Create;
bMain.putString(j('CONFIG_MODE'),j('UPDATE'));
bMain.putString(j('PROFILE_NAME'),j('test'));
bMain.putString(j('RESET_CONFIG'), j('true'));
bMain.putString(j('PROFILE_ENABLED'),j('true'));
bMain.putParcelableArray(j('APP_LIST'),TJavaObjectArray<JParcelable>(bAppList));
// Also tried that
// bMain.putParcelableArray(j('APP_LIST'),bAppList);
iSetConfig := TJIntent.Create;
iSetConfig.setAction(j('com.symbol.datawedge.api.ACTION'));
iSetConfig.putExtra(j('com.symbol.datawedge.api.SET_CONFIG'),bMain);
TAndroidHelper.Context.sendBroadcast(iSetConfig);
end;
运行此代码可以正常工作,只是应用程序列表 (bAppList) 不是在 DataWedge 应用程序中创建,而其他参数都已正确应用。我还成功创建了其他不涉及 ParcelableArray 的配置。
我终于找到了解释。该代码运行良好。但我有另一个配置文件在设备上使用相同的应用程序,并且您无法将同一应用程序与多个配置文件关联。因此删除现有的配置文件解决了问题。
我爱你。我有同样的问题。谢谢!