【发布时间】:2018-10-04 11:26:52
【问题描述】:
在 delphi Tokyo 源代码中,我看到了这两种不同的方式来检索 “服务管理器”
var FLocationManager: JLocationManager;
FLocationManager := TJLocationManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.LOCATION_SERVICE));
和:
var FNotificationManager: JNotificationManager;
var NotificationServiceNative: JObject;
NotificationServiceNative := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION_SERVICE);
FNotificationManager := TJNotificationManager.Wrap((NotificationServiceNative as ILocalObject).GetObjectID);
这两种方式到底有什么区别?为什么在第二种情况下他们这样做 (NotificationServiceNative as ILocalObject).GetObjectID 而不是在第一种情况下?
【问题讨论】:
-
我认为第一个是错误的。在我自己的代码中,它看起来像这样:
LocationManagerService := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.LOCATION_SERVICE); FLocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID); -
是的,但在 delphi 源代码中,他们编写了 FLocationManager := TJLocationManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.LOCATION_SERVICE));在 System.Android.Sensors.pas
-
@loki 不会是 Delphi 源代码第一次以错误的方式访问 Android 对象
-
@RemyLebeau 我同意,但是 System.Android.Sensors.pas 是每个人都非常使用的单位,那么它是如何工作的?没有错误报告
-
@loki 实际上不,这不是错误。
Wrap()有 2 个重载,一个用于对象引用,一个用于对象 ID。这就是为什么这两个例子都有效的原因
标签: delphi firemonkey