2017-07-19 53 views
0

我已經使用Fused Location api獲取經度和緯度,但問題在於itsit的說法,谷歌api客戶端isntisn't連接,但我沒有。我不知道我錯在哪裏,幫助我。Fused Location Api位置

我飛濺的活動,其中II現在用電熔阿比::

注:: GPS追蹤器是這裏的班級,我用它使用我還沒有在節目中我用它的位置經理。我只是用它來檢查Gps是打開還是關閉。

我也在manifest文件中添加了所有權限。

package com.example.na462.sunshine; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.location.*; 
import android.location.Location; 
import android.os.AsyncTask; 
import android.os.Handler; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.FusedLocationProviderApi; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 

import java.net.URL; 

import static android.R.attr.animation; 
import static com.example.na462.sunshine.R.id.MainList; 

public class SplashActivity extends Activity implements Animation.AnimationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 
Animation animation, TextAnimation; 
ImageView imageView; 
ImageView ErrorImage; 
LocationRequest locationRequest; 
TextView AnimationText; 
Button ErrorReload; 
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi; 
private GoogleApiClient googleApiClient; 
LinearLayout Layout; 
Receiver receiver; 
boolean Connection; 
GPSTracker gpsTracker; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash); 

    googleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 

    locationRequest = new LocationRequest(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
    locationRequest.setInterval(60 * 1000); 
    locationRequest.setFastestInterval(15 * 1000); 


    AnimationText = (TextView) findViewById(R.id.TextAnimation); 
    ErrorReload = (Button) findViewById(R.id.ErrorReload); 
    imageView = (ImageView) (SplashActivity.this).findViewById(R.id.Animation); 
    Layout = (LinearLayout) findViewById(R.id.Error); 
    ErrorImage = (ImageView) findViewById(R.id.Nointernet); 
    gpsTracker = new GPSTracker(SplashActivity.this); 
    gpsTracker.getLocation(); 
    receiver = new Receiver(); 
    Connection = receiver.internetconnection(SplashActivity.this); 

    if (!Connection || !GPSEnabled.GPS) { 
     imageView.setVisibility(View.INVISIBLE); 
     Layout.setVisibility(View.VISIBLE); 
     ErrorImage.setVisibility(View.VISIBLE); 
     AnimationText.setVisibility(View.INVISIBLE); 

    } else { 
     AnimationText.setVisibility(View.VISIBLE); 
     imageView.setVisibility(View.VISIBLE); 
     Layout.setVisibility(View.INVISIBLE); 
     ErrorImage.setVisibility(View.INVISIBLE); 

     new Handler().postDelayed(new Runnable() { 
      public void run() { 
       googleApiClient.disconnect(); 
       Intent i = new Intent(SplashActivity.this, ScrollingActivity.class); 
       startActivity(i); 
       overridePendingTransition(R.anim.fade_in, R.anim.fade_out); 
       finish(); 
      } 
     }, 8000); 


     animation = AnimationUtils.loadAnimation(this, R.anim.fade_in); 
     animation.setAnimationListener(this); 
     imageView.startAnimation(animation); 


     TextAnimation = AnimationUtils.loadAnimation(this, R.anim.blink); 
     TextAnimation.setAnimationListener(this); 
     AnimationText.startAnimation(TextAnimation); 

     googleApiClient.connect(); 



    } 
} 


@Override 
public void onAnimationStart(Animation animation) { 

} 

@Override 
public void onAnimationEnd(Animation animation) { 

} 

@Override 
public void onAnimationRepeat(Animation animation) { 

} 

// Its an onclick Method For Retry if there isnt any conncection or GPS Estabished 



@Override 
protected void onStart() { 
    super.onStart(); 

} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if(googleApiClient.isConnected()) { 
     onLocationUpdates(); 
    } 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    googleApiClient.disconnect(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    onLocationUpdates(); 
} 

private void onLocationUpdates() { 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

@Override 
public void onLocationChanged(Location location) { 
    ValuesToPass.Latitude = location.getLatitude(); 
    ValuesToPass.Longitude = location.getLongitude(); 
    CordinatesLoc.Latitude = Math.round(location.getLatitude()); 
    CordinatesLoc.Longitude = Math.round(location.getLongitude()); 
} 
} 

回答

1

谷歌最近發佈了一個新的API,它使得這個過程變得更容易,而且你不再需要GoogleApiClient。我強烈建議你改用這個API。

請參閱下面的示例以瞭解如何使用它。

FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(context); 
client.requestLocationUpdates(locationRequest, pendingIntent) 
     .addOnCompleteListener(task -> { 
      Log.d("requestLocationUpdates", "result: " + task.getResult()); 
     }) 
     .addOnFailureListener(e -> { 
      String errorMessage; 
      if (e instanceof ApiException) { 
       errorMessage = ((ApiException) e).getStatusMessage(); 
      } else { 
       errorMessage = e.getMessage(); 
      } 
      Log.d("requestLocationUpdate", "failed: " + errorMessage); 
     }); 

在你的情況可能與this取代context指你的活動,而不是一個方面的參考。

pendingIntent是PendingIntent實例,它定義瞭如何處理收到的位置更新。

+0

我的應用程序從API級別9的目標,請幫助我的位置,融合API掛起時,我請求的位置。沒有收到任何回撥,如失敗或暫停或任何事情。 –

+0

不針對api 9.地球上沒有人使用api 9了 –

+0

感謝您的快速回復,不幸的是我必須使用這個,所以你有我的問題的任何解決方案?如果有的話請幫忙。 >>「當我請求位置時融合的API掛起,沒有收到任何回調,如失敗或暫停或任何其他事情」。定位服務處於高精度,請求間隔10秒。 –

相關問題