2017-08-10 21 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 –

相關問題