Recent

Author Topic: How can translate this java code: public interface AMapLocationListener{}  (Read 1087 times)

iwizard

  • New member
  • *
  • Posts: 8
I want to create a new lamw component, and when I use the tools provided by LAMW to import the jar, I am told that the interface cannot be imported, but the sample code of this jar indicates that I want to use the class inherited from the interface to complete the code like an event callback:

java code:
Code: Java  [Select][+][-]
  1. public interface AMapLocationListener {
  2.     void onLocationChanged(AMapLocation var1);
  3. }
Code: Java  [Select][+][-]
  1. AMapLocationListener locationListener = new AMapLocationListener() {
  2.         @Override
  3.         public void onLocationChanged(AMapLocation location) {
  4.                 if (null != location) {
  5.                         StringBuffer sb = new StringBuffer();
  6.                         if(location.getErrorCode() == 0){
  7.                                 sb.append("Longitude   : " + location.getLongitude() + "\n");
  8.                                 sb.append("Latitude    : " + location.getLatitude() + "\n");
  9.                                 sb.append("Accuracy    : " + location.getAccuracy() + "meter" + "\n");
  10.                                 String result = sb.toString();
  11.                                 tvResult.setText(result);
  12.                         } else {
  13.                                 tvResult.setText("locate error,loc is null");
  14.                         }
  15.                 }
  16. };

How do I translate the Java interface and its implementation classes into FreePascal? Please help me, thank you very much!

Oh yes, I used the tool under delphi to get the translation code for the interface, but it can't be used directly under freepascal:

Code: Pascal  [Select][+][-]
  1. JAMapLocationListenerClass = interface(JObjectClass)
  2.   ['{67513425-8988-4FE6-974B-41C46793B470}']
  3.     { static Property Methods }
  4.  
  5.     { static Methods }
  6.  
  7.     { static Property }
  8.   end;
  9.  
  10.   [JavaSignature('com/amap/api/location/AMapLocationListener')]
  11.   JAMapLocationListener = interface(IJavaInstance)
  12.   ['{03B66EBC-1491-4DAB-BB09-CDD59D99272B}']
  13.     { Property Methods }
  14.  
  15.     { methods }
  16.     procedure onLocationChanged(P1: JAMapLocation); cdecl;
  17.  
  18.     { Property }
  19.   end;
  20.  
  21.   TJAMapLocationListener = class(TJavaGenericImport<JAMapLocationListenerClass, JAMapLocationListener>) end;

The attachment is the jar file I want to import.


jmpessoa

  • Hero Member
  • *****
  • Posts: 2317
Re: How can translate this java code: public interface AMapLocationListener{}
« Reply #1 on: February 17, 2024, 08:38:03 pm »

What is the link to the original java use/example?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

iwizard

  • New member
  • *
  • Posts: 8
Re: How can translate this java code: public interface AMapLocationListener{}
« Reply #2 on: February 18, 2024, 12:58:22 am »
Thank you for your reply. The instance code is as follows, please refer to the "initLocation()" method:

demo download url : https://pan.baidu.com/s/1elevPfZAKC7FkIF9ZMKklg?pwd=mhex

Code: Java  [Select][+][-]
  1. package com.amap.location.demo;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Notification;
  5. import android.app.NotificationChannel;
  6. import android.app.NotificationManager;
  7. import android.content.Context;
  8. import android.graphics.Color;
  9. import android.os.Bundle;
  10. import android.text.TextUtils;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.CheckBox;
  15. import android.widget.EditText;
  16. import android.widget.RadioGroup;
  17. import android.widget.RadioGroup.OnCheckedChangeListener;
  18. import android.widget.TextView;
  19.  
  20. import com.amap.api.location.AMapLocation;
  21. import com.amap.api.location.AMapLocationClient;
  22. import com.amap.api.location.AMapLocationClientOption;
  23. import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
  24. import com.amap.api.location.AMapLocationClientOption.AMapLocationProtocol;
  25. import com.amap.api.location.AMapLocationListener;
  26. import com.amap.api.location.AMapLocationQualityReport;
  27.  
  28. /**
  29.  * Created by hongming.wang on 2018/1/29.
  30.  * 后台定位示例
  31.  * <p>
  32.  * 从Android 8.0开始,Android系统为了实现低功耗,Android 8.0系统对后台应用获取位置的频率进行了限制,只允许每小时几次位置更新。
  33.  * 根据Android 8.0的开发指引,为了适配这一系统特性,
  34.  * 高德定位SDK从8.0开始增加了两个新接口enableBackgroundLocation和disableBackgroundLocation用来控制是否开启后台定位。
  35.  * 开启后sdk会生成一个前台服务通知,告知用户应用正在后台运行,使得开发者自己的应用退到后台的时候,仍有前台通知在,提高应用切入后台后位置更新的频率。
  36.  * 如果您的应用在退到后台时本身就有前台服务通知,则无需按照本示例的介绍做适配。<br>
  37.  * 示例中提供了两种方法启动和关闭后台定位功能,请根据业务场景进行相应的修改<br>
  38.  * 1、通过按钮触发,点击按钮调用相应的接口开开启或者关闭后台定位,此种方法主要是更直观的展示后台定位的功能
  39.  * 2、通过生命周期判断APP是否处于后台,当处于后台时才开启后台定位功能,恢复到前台后关闭后台定位功能
  40.  * </p>
  41.  */
  42. public class Location_BackGround_Activity extends CheckPermissionsActivity
  43.                 implements
  44.                         OnClickListener{
  45.         private TextView tvResult;
  46.         private Button btLocation;
  47.         private Button btEnableBackgroundLocation;
  48.         private Button btDisableBackgroundLocation;
  49.  
  50.         private AMapLocationClient locationClient = null;
  51.         private AMapLocationClientOption locationOption = null;
  52.  
  53.         @Override
  54.         protected void onCreate(Bundle savedInstanceState) {
  55.                 super.onCreate(savedInstanceState);
  56.                 setContentView(R.layout.activity_location_background);
  57.                 setTitle(R.string.title_locationBackground);
  58.                
  59.                 initView();
  60.  
  61.                 initLocation();
  62.         }
  63.  
  64.  
  65.         @Override
  66.         protected void onResume() {
  67.                 super.onResume();
  68.                 //切入前台后关闭后台定位功能
  69.                 if(null != locationClient) {
  70.                         locationClient.disableBackgroundLocation(true);
  71.                 }
  72.         }
  73.  
  74.  
  75.         @Override
  76.         protected void onStop() {
  77.                 super.onStop();
  78.                 boolean isBackground = ((MyApplication)getApplication()).isBackground();
  79.                 //如果app已经切入到后台,启动后台定位功能
  80.                 if(isBackground){
  81.                         if(null != locationClient) {
  82.                                 locationClient.enableBackgroundLocation(2001, buildNotification());
  83.                         }
  84.                 }
  85.         }
  86.  
  87.         @Override
  88.         protected void onPause() {
  89.                 super.onPause();
  90.         }
  91.  
  92.         //初始化控件
  93.         private void initView(){
  94.  
  95.                 tvResult = (TextView) findViewById(R.id.tv_result);
  96.                 btLocation = (Button) findViewById(R.id.bt_location);
  97.                 btEnableBackgroundLocation = (Button) findViewById(R.id.bt_enableBackground);
  98.                 btDisableBackgroundLocation = (Button) findViewById(R.id.bt_disableBackground);
  99.  
  100.                 btLocation.setOnClickListener(this);
  101.                 btEnableBackgroundLocation.setOnClickListener(this);
  102.                 btDisableBackgroundLocation.setOnClickListener(this);
  103.         }
  104.        
  105.         @Override
  106.         protected void onDestroy() {
  107.                 super.onDestroy();
  108.                 destroyLocation();
  109.         }
  110.  
  111.         @Override
  112.         public void onClick(View v) {
  113.                 if (v.getId() == R.id.bt_location) {
  114.                         if (btLocation.getText().equals(
  115.                                         getResources().getString(R.string.startLocation))) {
  116.                                 btLocation.setText(getResources().getString(
  117.                                                 R.string.stopLocation));
  118.                                 tvResult.setText("正在定位...");
  119.                                 startLocation();
  120.                         } else {
  121.                                 btLocation.setText(getResources().getString(
  122.                                                 R.string.startLocation));
  123.                                 stopLocation();
  124.                                 tvResult.setText("定位停止");
  125.                         }
  126.                 }
  127.  
  128.                 if(v.getId() == R.id.bt_enableBackground){
  129.                         if(null == locationClient){
  130.                                 try {
  131.                                         locationClient = new AMapLocationClient(this);
  132.                                         //启动后台定位
  133.                                         locationClient.enableBackgroundLocation(2001, buildNotification());
  134.                                 } catch (Exception e) {
  135.                                         e.printStackTrace();
  136.                                 }
  137.                         }
  138.  
  139.                 }
  140.  
  141.                 if(v.getId() == R.id.bt_disableBackground){
  142.                         if(null == locationClient){
  143.                                 try {
  144.                                         locationClient = new AMapLocationClient(this);
  145.                                         //关闭后台定位
  146.                                         locationClient.disableBackgroundLocation(true);
  147.                                 } catch (Exception e) {
  148.                                         e.printStackTrace();
  149.                                 }
  150.                         }
  151.  
  152.                 }
  153.         }
  154.        
  155.         /**
  156.          * 初始化定位
  157.          *
  158.          * @since 2.8.0
  159.          * @author hongming.wang
  160.          *
  161.          */
  162.         private void initLocation(){
  163.                 //初始化client
  164.                 try {
  165.                         locationClient = new AMapLocationClient(this.getApplicationContext());
  166.                         locationOption = getDefaultOption();
  167.                         //设置定位参数
  168.                         locationClient.setLocationOption(locationOption);
  169.                         // 设置定位监听
  170.                         locationClient.setLocationListener(locationListener);
  171.                 } catch (Exception e) {
  172.                         e.printStackTrace();
  173.                 }
  174.  
  175.         }
  176.        
  177.         /**
  178.          * 默认的定位参数
  179.          * @since 2.8.0
  180.          * @author hongming.wang
  181.          *
  182.          */
  183.         private AMapLocationClientOption getDefaultOption(){
  184.                 AMapLocationClientOption mOption = new AMapLocationClientOption();
  185.                 mOption.setLocationMode(AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
  186.                 mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
  187.                 mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
  188.                 mOption.setInterval(2000);//可选,设置定位间隔。默认为2秒
  189.                 mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
  190.                 mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false
  191.                 mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
  192.                 AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
  193.                 mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false
  194.                 mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差
  195.                 mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true
  196.                 return mOption;
  197.         }
  198.        
  199.         /**
  200.          * 定位监听
  201.          */
  202.         AMapLocationListener locationListener = new AMapLocationListener() {
  203.                 @Override
  204.                 public void onLocationChanged(AMapLocation location) {
  205.                         if (null != location) {
  206.  
  207.                                 StringBuffer sb = new StringBuffer();
  208.                                 //errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
  209.                                 if(location.getErrorCode() == 0){
  210.                                         sb.append("定位成功" + "\n");
  211.                                         sb.append("定位类型: " + location.getLocationType() + "\n");
  212.                                         sb.append("经    度    : " + location.getLongitude() + "\n");
  213.                                         sb.append("纬    度    : " + location.getLatitude() + "\n");
  214.                                         sb.append("精    度    : " + location.getAccuracy() + "米" + "\n");
  215.                                         sb.append("提供者    : " + location.getProvider() + "\n");
  216.  
  217.                                         sb.append("速    度    : " + location.getSpeed() + "米/秒" + "\n");
  218.                                         sb.append("角    度    : " + location.getBearing() + "\n");
  219.                                         // 获取当前提供定位服务的卫星个数
  220.                                         sb.append("星    数    : " + location.getSatellites() + "\n");
  221.                                         sb.append("国    家    : " + location.getCountry() + "\n");
  222.                                         sb.append("省            : " + location.getProvince() + "\n");
  223.                                         sb.append("市            : " + location.getCity() + "\n");
  224.                                         sb.append("城市编码 : " + location.getCityCode() + "\n");
  225.                                         sb.append("区            : " + location.getDistrict() + "\n");
  226.                                         sb.append("区域 码   : " + location.getAdCode() + "\n");
  227.                                         sb.append("地    址    : " + location.getAddress() + "\n");
  228.                                         sb.append("地    址    : " + location.getDescription() + "\n");
  229.                                         sb.append("兴趣点    : " + location.getPoiName() + "\n");
  230.                                         //定位完成的时间
  231.                                         sb.append("定位时间: " + Utils.formatUTC(location.getTime(), "yyyy-MM-dd HH:mm:ss") + "\n");
  232.                                 } else {
  233.                                         //定位失败
  234.                                         sb.append("定位失败" + "\n");
  235.                                         sb.append("错误码:" + location.getErrorCode() + "\n");
  236.                                         sb.append("错误信息:" + location.getErrorInfo() + "\n");
  237.                                         sb.append("错误描述:" + location.getLocationDetail() + "\n");
  238.                                 }
  239.                                 sb.append("***定位质量报告***").append("\n");
  240.                                 sb.append("* WIFI开关:").append(location.getLocationQualityReport().isWifiAble() ? "开启":"关闭").append("\n");
  241.                                 sb.append("* GPS状态:").append(getGPSStatusString(location.getLocationQualityReport().getGPSStatus())).append("\n");
  242.                                 sb.append("* GPS星数:").append(location.getLocationQualityReport().getGPSSatellites()).append("\n");
  243.                                 sb.append("****************").append("\n");
  244.                                 //定位之后的回调时间
  245.                                 sb.append("回调时间: " + Utils.formatUTC(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "\n");
  246.  
  247.                                 //解析定位结果,
  248.                                 String result = sb.toString();
  249.                                 tvResult.setText(result);
  250.                         } else {
  251.                                 tvResult.setText("定位失败,loc is null");
  252.                         }
  253.                 }
  254.         };
  255.  
  256.  
  257.         /**
  258.          * 获取GPS状态的字符串
  259.          * @param statusCode GPS状态码
  260.          * @return
  261.          */
  262.         private String getGPSStatusString(int statusCode){
  263.                 String str = "";
  264.                 switch (statusCode){
  265.                         case AMapLocationQualityReport.GPS_STATUS_OK:
  266.                                 str = "GPS状态正常";
  267.                                 break;
  268.                         case AMapLocationQualityReport.GPS_STATUS_NOGPSPROVIDER:
  269.                                 str = "手机中没有GPS Provider,无法进行GPS定位";
  270.                                 break;
  271.                         case AMapLocationQualityReport.GPS_STATUS_OFF:
  272.                                 str = "GPS关闭,建议开启GPS,提高定位质量";
  273.                                 break;
  274.                         case AMapLocationQualityReport.GPS_STATUS_MODE_SAVING:
  275.                                 str = "选择的定位模式中不包含GPS定位,建议选择包含GPS定位的模式,提高定位质量";
  276.                                 break;
  277.                         case AMapLocationQualityReport.GPS_STATUS_NOGPSPERMISSION:
  278.                                 str = "没有GPS定位权限,建议开启gps定位权限";
  279.                                 break;
  280.                 }
  281.                 return str;
  282.         }
  283.  
  284.         /**
  285.          * 开始定位
  286.          *
  287.          * @since 2.8.0
  288.          * @author hongming.wang
  289.          *
  290.          */
  291.         private void startLocation(){
  292.                 try {
  293.                         // 设置定位参数
  294.                         locationClient.setLocationOption(locationOption);
  295.                         // 启动定位
  296.                         locationClient.startLocation();
  297.                 } catch (Exception e) {
  298.                         e.printStackTrace();
  299.                 }
  300.  
  301.         }
  302.        
  303.         /**
  304.          * 停止定位
  305.          *
  306.          * @since 2.8.0
  307.          * @author hongming.wang
  308.          *
  309.          */
  310.         private void stopLocation(){
  311.                 try {
  312.                         // 停止定位
  313.                         locationClient.stopLocation();
  314.                 } catch (Exception e) {
  315.                         e.printStackTrace();
  316.                 }
  317.  
  318.         }
  319.  
  320.         @Override
  321.         public void onBackPressed() {
  322.                 super.onBackPressed();
  323.         }
  324.  
  325.         /**
  326.          * 销毁定位
  327.          *
  328.          * @since 2.8.0
  329.          * @author hongming.wang
  330.          *
  331.          */
  332.         private void destroyLocation(){
  333.                 if (null != locationClient) {
  334.                         /**
  335.                          * 如果AMapLocationClient是在当前Activity实例化的,
  336.                          * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
  337.                          */
  338.                         locationClient.onDestroy();
  339.                         locationClient = null;
  340.                         locationOption = null;
  341.                 }
  342.         }
  343.  
  344.         private void createNotificationChannel(){
  345.  
  346.         }
  347.  
  348.  
  349.         private static final String NOTIFICATION_CHANNEL_NAME = "BackgroundLocation";
  350.         private NotificationManager notificationManager = null;
  351.         boolean isCreateChannel = false;
  352.         @SuppressLint("NewApi")
  353.         private Notification buildNotification() {
  354.  
  355.                 Notification.Builder builder = null;
  356.                 Notification notification = null;
  357.                 if(android.os.Build.VERSION.SDK_INT >= 26) {
  358.                         //Android O上对Notification进行了修改,如果设置的targetSDKVersion>=26建议使用此种方式创建通知栏
  359.                         if (null == notificationManager) {
  360.                                 notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  361.                         }
  362.                         String channelId = getPackageName();
  363.                         if(!isCreateChannel) {
  364.                                 NotificationChannel notificationChannel = new NotificationChannel(channelId,
  365.                                                 NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
  366.                                 notificationChannel.enableLights(true);//是否在桌面icon右上角展示小圆点
  367.                                 notificationChannel.setLightColor(Color.BLUE); //小圆点颜色
  368.                                 notificationChannel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
  369.                                 notificationManager.createNotificationChannel(notificationChannel);
  370.                                 isCreateChannel = true;
  371.                         }
  372.                         builder = new Notification.Builder(getApplicationContext(), channelId);
  373.                 } else {
  374.                         builder = new Notification.Builder(getApplicationContext());
  375.                 }
  376.                 builder.setSmallIcon(R.drawable.ic_launcher)
  377.                                 .setContentTitle(Utils.getAppName(this))
  378.                                 .setContentText("正在后台运行")
  379.                                 .setWhen(System.currentTimeMillis());
  380.  
  381.                 if (android.os.Build.VERSION.SDK_INT >= 16) {
  382.                         notification = builder.build();
  383.                 } else {
  384.                         return builder.getNotification();
  385.                 }
  386.                 return notification;
  387.         }
  388.  
  389. }
  390.  
« Last Edit: February 18, 2024, 02:28:49 am by iwizard »

 

TinyPortal © 2005-2018