首先,很抱歉问这个愚蠢的问题。由于我是Android的新手,所以我正在尝试制作一个应用程序,其中首先会打开对话框,以请求像谷歌地图或ola一样打开GPS的权限。然后在那之后,我想获取用户的纬度和经度,但不幸的是,我尝试并搜索了这么多链接,但我没有得到正确的解决方案。所以谁能帮我。
我的代码是:
public class GpsPlaceSearch extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener
{
Activity context;
GoogleApiClient googleApiClient, getGoogleApiClient;
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
LocationRequest locationRequest1;
Location location;
private double currentLatitude;
private double currentLongitude;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111;
public GpsPlaceSearch(){
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps_place_search);
getGoogleApiClient = new GoogleApiClient.Builder(this)
class will handle connection stuff
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
locationRequest1=locationRequests();
}
public GpsPlaceSearch(Activity context){
this.context=context;
googleApiClient=getInstance();
if (googleApiClient!=null){
settingsrequest();
googleApiClient.connect();
}
}
public LocationRequest locationRequests(){
LocationRequest locationRequest=LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
return locationRequest;
}
public GoogleApiClient getInstance(){
GoogleApiClient mGoogleApiClient=new GoogleApiClient.Builder(context).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
return mGoogleApiClient;
}
public void settingsrequest(){
Log.e("settingsrequest","Comes");
locationRequest1=locationRequests();
LocationSettingsRequest.Builder builder=new LocationSettingsRequest.Builder().addLocationRequest(locationRequest1);
builder.setAlwaysShow(true);
final PendingResult<LocationSettingsResult> result= LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
Status status=result.getStatus();
LocationSettingsStates states=result.getLocationSettingsStates();
switch (status.getStatusCode()){
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try{
status.startResolutionForResult(context,REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
});
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onConnected(Bundle bundle) {
location=LocationServices.FusedLocationApi.getLastLocation(getGoogleApiClient);
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},108);
LocationServices.FusedLocationApi.requestLocationUpdates(getGoogleApiClient,locationRequest1,this);
}
else {
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
Toast.makeText(this, currentLatitude + " AND " + currentLongitude + "", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()){
try {
connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
else {
Log.e("Error", "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
@Override
protected void onStart() {
super.onStart();
Activity mActivity=this;
GpsPlaceSearch gpsPlaceSearch=new GpsPlaceSearch(mActivity);
}
@Override
protected void onResume() {
super.onResume();
//Now lets connect to the API
getGoogleApiClient.connect();
}
@Override
protected void onPause() {
super.onPause();
if (getGoogleApiClient.isConnected()){
LocationServices.FusedLocationApi.removeLocationUpdates(getGoogleApiClient,this);
getGoogleApiClient.disconnect();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
LocationSettingsStates states=LocationSettingsStates.fromIntent(data);
switch (requestCode){
case REQUEST_CHECK_SETTINGS:
switch (resultCode){
case Activity.RESULT_OK:
break;
case Activity.RESULT_CANCELED:
startActivity(new Intent(getApplicationContext(),Login_page.class));
break;
}
break;
}
}
@Override
public void onLocationChanged(Location location) {
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
Toast.makeText(this, currentLatitude + " WORKS " + currentLongitude + "", Toast.LENGTH_LONG).show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_FINE_LOCATION:
if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
}
else {
}
return;
}
}
}
日志猫 :
致命例外:主要 进程: com.bigfoot.trialapp, PID: 17660 java.lang.IllegalArgumentException: GoogleApiClient 参数为 必填。 at com.google.android.gms.common.internal.zzaa.zzb(未知来源) at com.google.android.gms.location.LocationServices.zzj(Unknown 来源) 在 com.google.android.gms.location.internal.zzd.getLastLocation(Unknown 来源) 在 com.bigfoot.trialapp.GpsPlaceSearch.onConnected(GpsPlaceSearch.java:128) at com.google.android.gms.common.internal.zzk.zzp(未知来源) at com.google.android.gms.internal.zzrd.zzn(未知来源) at com.google.android.gms.internal.zzrb.zzass(未知来源) at com.google.android.gms.internal.zzrb.onConnected(未知来源) at com.google.android.gms.internal.zzrf.onConnected(未知来源) at com.google.android.gms.internal.zzqr.onConnected(未知来源) at com.google.android.gms.common.internal.zzj$1.onConnected(Unknown 来源) at com.google.android.gms.common.internal.zze$zzj.zzavj(Unknown 来源) at com.google.android.gms.common.internal.zze$zza.zzc(未知来源) at com.google.android.gms.common.internal.zze$zza.zzv(未知来源) at com.google.android.gms.common.internal.zze$zze.zzavl(Unknown 来源) 在 com.google.android.gms.common.internal.zze$zzd.handleMessage(Unknown 来源) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
您应该从onConnected中删除以下行
location=LocationServices.FusedLocationApi.getLastLocation(getGoogleApiClient);
并且应该只在onConnected中调用requestLocationUpdates方法
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onConnected(Bundle bundle) {
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},108);
LocationServices.FusedLocationApi.requestLocationUpdates(getGoogleApiClient,locationRequest1,this);
}
}else{
//custom dialog for "please enable location permission to access device location"
}