我使用了Fused Location api来获取经度和经度,但问题在于它说谷歌api客户端没有连接但我没有。我不知道我哪里错了,请帮助我。
My Splash Activity我在使用Fused Api ::
注意:: GPS Tracker是我在这里使用的一个类,它使用位置管理器我还没有在程序i中使用它。我只是用它来检查Gps是打开还是关闭。
我也在清单文件中添加了所有权限。
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());
}
}
答案 0 :(得分:3)
请参阅下面的示例了解如何使用它。
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);
});
在您的情况下,您可以将context
替换为this
来引用您的活动而不是上下文参考。
pendingIntent
是PendingIntent实例,用于定义如何处理收到的位置更新。