2017-08-10 23 views
2

我试图通过本机接口(android)在cn1中应用FusedLocation提供程序。我已经实现了ConnectionCallbacks和OnConnectionFailedListener接口。它在native android中生成onConnected(),onConnectionSuspended()和onConnectionFailed()方法,这些方法在cn1中构建应用程序时不起作用。已实现的方法不适用于本地接口(android)熔合位置提供程序

此外,像onResume,onDestroy等FusedLocationImpl等生命周期方法也不起作用。在未来的日子里,我打算为通用目的创建融合的GPS库。

PS。调试时没有生成错误,也没有任何其他错误。

FusedLocationImpl.java

import com.codename1.impl.android.AndroidNativeUtil; 
import android.os.Bundle; 
import android.location.Location; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationAvailability; 
import com.google.android.gms.location.LocationCallback; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationResult; 
import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.location.LocationListener; 

public class FusedLocationImpl implements GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener { 

    private GoogleApiClient mGoogleApiClient; 
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000; 
    private Location mLastLocation; 
    private LocationRequest mLocationRequest; 

    public boolean isSupported() { 
     return true; 
    } 

    public void getFusedLocationPermission() { 
     if (!com.codename1.impl.android.AndroidNativeUtil.checkForPermission(Manifest.permission.ACCESS_FINE_LOCATION, "Please allow location permission")) { 
     } 
    } 

    public void fusedLocation() { 
     if (checkPlayServices()) { 
      buildGoogleApiClient(); 
     } 
    } 

    public void onConnected(Bundle bundle) { // not working... 
     Log.i("onConnected", "GoogleApiClient connected!"); 
     createLocationRequest(); 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     Log.i("onConnected", " Location: " + mLastLocation); 
    } 

    protected void createLocationRequest() { // not working... 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(10000); 
     mLocationRequest.setFastestInterval(5000); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, new LocationCallback() { 
      @Override 
      public void onLocationResult(final LocationResult locationResult) { 
       Log.i("onLocationResult",locationResult + ""); 
      } 

      @Override 
      public void onLocationAvailability(LocationAvailability locationAvailability) { 
       Log.i("onLocationAvailability", "onLocationAvailability: isLocationAvailable = " + locationAvailability.isLocationAvailable()); 
      } 
     }, null); 
    } 

    public void onConnectionSuspended(int i) { 
     mGoogleApiClient.connect(); 
    } 

    public void onConnectionFailed(ConnectionResult result) { 
     Log.i("onConnectionFailed", "Connection failed: ConnectionResult.getErrorCode() = " 
       + result.getErrorCode()); 
    } 

    public void onPause() { 
     if (mGoogleApiClient.isConnected()) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (LocationListener) this); 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    public void onResume() { 
     checkPlayServices(); 
    } 

    public void onStart() { 
     if (mGoogleApiClient != null) { 
      mGoogleApiClient.connect(); 
      Log.i("onStart", "mGoogleApiClient.connect()"); 
     } 
    } 

    public void onDestroy() { 
     Log.i("onDestory", "Service destroyed!"); 
     mGoogleApiClient.disconnect(); 
    } 

    private boolean checkPlayServices() { 
     int resultCode = GooglePlayServicesUtil 
       .isGooglePlayServicesAvailable(AndroidNativeUtil.getActivity()); 
     if (resultCode != ConnectionResult.SUCCESS) { 
      if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { 
       GooglePlayServicesUtil.getErrorDialog(resultCode, AndroidNativeUtil.getActivity(), 
         PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
      } else { 
       AndroidNativeUtil.getActivity().finish(); 
      } 
      return false; 
     } 
     return true; 
    } 

    protected synchronized void buildGoogleApiClient() { 
     Log.i("buildGoogleApiClient", "Building GoogleApiClient"); 

     mGoogleApiClient = new GoogleApiClient.Builder(AndroidNativeUtil.getActivity()) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 
} 

FusedLocation.java

public interface FusedLocation extends NativeInterface{ 
    public void getFusedLocationPermission(); 
    public void fusedLocation(); 
} 

MyApplication.java

public void start() { 
    if (current != null) { 
     current.show(); 
     return; 
    } 

    Form hi = new Form("Hi World", BoxLayout.y()); 
    hi.show(); 

    Button btn = new Button("ask for permission"); 
    FusedLocation fl = (FusedLocation) NativeLookup.create(FusedLocation.class); 

    Button btn1 = new Button("fused network"); 
    hi.add(btn1); 
    btn1.addActionListener(e->{ 
     if (fl != null && fl.isSupported()) { 
      fl.getFusedLocationPermission(); 
      fl.fusedLocation(); 
      System.out.println("fusedLocation"); 
     } 
    }); 
} 

PS。位置权限(ACCESS_FINE_LOCATION)成功。我得到的主要问题是onConnected()和其他实现的方法不被调用。三江源

CN1位置经理:这不是给人相当的性能,本机融合位置提供

public final void checkGPS() { 
    if (Display.getInstance().getLocationManager().isGPSDetectionSupported()) { 
     if (Display.getInstance().getLocationManager().isGPSEnabled()) { 
      InfiniteProgress ip = new InfiniteProgress(); 
      final Dialog ipDlg = ip.showInifiniteBlocking(); 
      //Cancel after 20 seconds 
      Location loc = LocationManager.getLocationManager().getCurrentLocationSync(20000); 
      ipDlg.dispose(); 
      if (loc != null) { 
       lat = loc.getLatitude(); 
       lng = loc.getLongitude(); 
       Dialog.show("location", "lat: " + lat + " lon: " + lng, "ok", null); 
      } else { 
       Dialog.show("GPS error", "Your location could not be found, please try going outside for a better GPS signal", "Ok", null); 
      } 
     } else { 
      Dialog.show("GPS disabled", "AppName needs access to GPS. Please enable GPS", "Ok", null); 
     } 
    } else { 
     Dialog.show("Warning", "GPS is not supported in your device", "ok", null); 
    } 
} 
+0

您是否通过Android Studio在设备上进行调试? –

+0

yeahh我在android studio中调试它 – beck

回答

0

我不知道为什么你不使用它在内部使用融合的位置位置监听器播放服务启用时(默认)。

您向XML请求权限,但未使用需要a different syntax的Android 6+权限。同样,如果您只是使用位置API,这是全部无缝的...

+0

在大多数情况下,融合位置可以提供更好的电池性能,以及更准确的适当性。其实我已经使用了cn1的LocationManager。但它经常返回空值。所以我想要做的是如果cn1 LocationManager给null,我正在使用融合位置。我已经在原生android中测试过它,我印象深刻。 – beck

+0

我上面已经提到我已经完成Android 6+权限,只是没有显示上面的代码(好吧,我现在也添加了)。我们在cn1中有融合位置吗?你能说清楚一点吗?顺便说一下,我很困惑,为什么GoogleApiClient.ConnectionCallbacks方法不工作? (例如:public void onConnected(Bundle bundle)不起作用 – beck

+0

如果您将Codename One作为默认值并使用我们的位置侦听器默认情况下使用了融合位置,因此您不需要任何本机代码,请参阅https:// github.com/codenameone/CodenameOne/blob/master/Ports/Android/src/com/codename1/location/AndroidLocationPlayServiceManager.java –

相关问题