我正在使用GoogleAPI客户端和locationRequest类来实现这一点,但没有显示对话,在logcat中它显示错误“缺少R.string.google_key”,我需要帮助如何实现这个以及需要哪个谷歌密钥此
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
GoogleApiClient googleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
//**************************
builder.setAlwaysShow(true); //this is the key ingredient
//**************************
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// // a dialog.
// try {
// // Show the dialog by calling startResolutionForResult(),
// // and check the result in onActivityResult().
// status.startResolutionForResult(
// Activity.class, 1000);
// } catch (IntentSender.SendIntentException e) {
// // Ignore the error.
// }
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
}); }
}
@Override
protected void onStart() {
super.onStart();
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
答案 0 :(得分:0)
Try below code its perfectly working for me
private static int REQUEST_CODE_RECOVER_PLAY_SERVICES = 200;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private AppLocationService appLocationService;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.reserve_user_screen);
appLocationService = new AppLocationService(ReserveUser.this);
showLocationAlertDialog(ReserveUser.this);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
@SuppressWarnings("unchecked")
public void showLocationAlertDialog(final Activity activity) {
if (android.os.Build.VERSION.SDK_INT >= 19) {
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
mGoogleApiClient.connect();
Object obj = LocationRequest.create();
((LocationRequest) (obj)).setPriority(100);
((LocationRequest) (obj)).setInterval(30000L);
((LocationRequest) (obj)).setFastestInterval(5000L);
obj = new LocationSettingsRequest.Builder().addLocationRequest(
((LocationRequest) (obj))).setAlwaysShow(true);
LocationServices.SettingsApi.checkLocationSettings(
mGoogleApiClient,
((LocationSettingsRequest.Builder) (obj)).build())
.setResultCallback(
new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(
LocationSettingsResult locationsettingsresult) {
// TODO Auto-generated method stub
Status status;
Log.e("result",
(new StringBuilder())
.append("=")
.append(locationsettingsresult
.getStatus())
.toString());
status = locationsettingsresult
.getStatus();
locationsettingsresult
.getLocationSettingsStates();
status.getStatusCode();
try {
status.startResolutionForResult(
ReserveUser.this, 1000);
} catch (SendIntentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
return;
} else {
startActivityForResult(new Intent(
"android.settings.LOCATION_SOURCE_SETTINGS"), 1);
return;
}
}
@Override
protected void onStart() {
super.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(20000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
/ *** * *应用程序位置类 * /
public class AppLocationService extends Service实现LocationListener {
protected LocationManager locationManager;
Location location;
private static final long MIN_DISTANCE_FOR_UPDATE = 10;
private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;
public AppLocationService(Context context) {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
}
public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider,
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
return location;
}
}
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}