Android 用弹出窗口提示用户打开GPS
所以我注意到谷歌地图能够提示用户启用GPS。我在下面尝试了这个方法,我得到了一次提示,然后什么都没有。。。为什么不再调用onActivityResult()方法Android 用弹出窗口提示用户打开GPS,android,gps,location,Android,Gps,Location,所以我注意到谷歌地图能够提示用户启用GPS。我在下面尝试了这个方法,我得到了一次提示,然后什么都没有。。。为什么不再调用onActivityResult()方法 public void checkLocationEnable() { Log.e(TAG, "Here"); LocationRequest mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(10000); mL
public void checkLocationEnable() {
Log.e(TAG, "Here");
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
final PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,
builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
final LocationSettingsStates state = locationSettingsResult.getLocationSettingsStates();
Log.e(TAG, "state:" + state);
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.e(TAG, "HERE-1");
// All location settings are satisfied. The client can
// initialize location requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.e(TAG, "REQUIRED");
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
getActivity(),
GenericActivity.REQUEST_LOCATION);
} 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.
Log.e(TAG, "UNAVALAIBLE");
break;
}
}
});
}
public void checkLocationEnable(){
Log.e(标记“此处”);
LocationRequest MLLocationRequest=新的LocationRequest();
mlLocationRequest.setInterval(10000);
mlLocationRequest.SetFastTestInterval(5000);
mLocationRequest.setPriority(位置请求.优先级高精度);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder()
.addLocationRequest(MLLocationRequest);
最终未决结果=
LocationServices.SettingsApi.checkLocationSettings(MGoogleAppClient,
builder.build());
result.setResultCallback(新的ResultCallback(){
@凌驾
public void onResult(@NonNull LocationSettingsResult LocationSettingsResult){
最终状态状态=locationSettingsResult.getStatus();
final LocationSettingsStates状态=locationSettingsResult.getLocationSettingsStates();
Log.e(标签“状态:+状态”);
开关(status.getStatusCode()){
案例位置设置StatusCodes.SUCCESS:
日志e(标签“此处-1”);
//满足所有位置设置。客户端可以
//在此处初始化位置请求。
打破
案例位置设置StatusCodes.RESOLUTION_要求:
日志e(标签“必需”);
//不满足位置设置,但可以修复此问题
//通过向用户显示一个对话框。
试一试{
//通过调用startResolutionForResult()显示对话框,
//并在onActivityResult()中检查结果。
status.StartResult解决方案(
getActivity(),
一般活动。请求位置);
}catch(IntentSender.sendtintentexe){
//忽略错误。
}
打破
案例位置设置StatusCodes.SETTINGS\u CHANGE\u不可用:
//位置设置不满意。但是,我们没有办法
//修复设置,使我们不会显示对话框。
Log.e(标签“不可用”);
打破
}
}
});
}
据我所知,您尝试启用该位置
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
break;
case Activity.RESULT_CANCELED:
break;
}
break;
}
}
一步一步的过程
首先,您需要设置GoogleAppClient并实现GoogleAppClient回调方法
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
mGoogleApiClient.connect();
}
回调方法
@Override
public void onConnected(Bundle bundle) {
Log.d("OnConnected", "Connection successful");
settingsrequest();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
}
一旦GoogleAppClient连接,onconnected方法将调用,settingsRequest方法将调用
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
LocationRequest locationRequest;
public void settingsrequest() {
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setNumUpdates(1);
locationRequest.setExpirationDuration(20000);
locationRequest.setFastestInterval(500);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true); //this is the key ingredient
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 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: {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}
// 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(MainTab.this, REQUEST_CHECK_SETTINGS);
} 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;
}
}
});
}
LocationServices.FusedLocationApi.requestLocationUpdates(mgoogleapClient,locationRequest,this)将为位置设置侦听器
当位置更改时,onLocationChanged方法将调用
@Override
public void onLocationChanged(Location location) {
prevLocation = location;
//Do you work
}
希望这将对您有所帮助。据我所知,您尝试启用该位置
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
break;
case Activity.RESULT_CANCELED:
break;
}
break;
}
}
一步一步的过程
首先,您需要设置GoogleAppClient并实现GoogleAppClient回调方法
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
mGoogleApiClient.connect();
}
回调方法
@Override
public void onConnected(Bundle bundle) {
Log.d("OnConnected", "Connection successful");
settingsrequest();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
}
一旦GoogleAppClient连接,onconnected方法将调用,settingsRequest方法将调用
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
LocationRequest locationRequest;
public void settingsrequest() {
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setNumUpdates(1);
locationRequest.setExpirationDuration(20000);
locationRequest.setFastestInterval(500);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true); //this is the key ingredient
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 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: {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}
// 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(MainTab.this, REQUEST_CHECK_SETTINGS);
} 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;
}
}
});
}
LocationServices.FusedLocationApi.requestLocationUpdates(mgoogleapClient,locationRequest,this)将为位置设置侦听器
当位置更改时,onLocationChanged方法将调用
@Override
public void onLocationChanged(Location location) {
prevLocation = location;
//Do you work
}
希望这会对您有所帮助。您需要覆盖onActivityResult方法,以便当用户响应对话框时,此方法将收到结果。我会在ActivityHome中覆盖它,以满足您的要求。您需要覆盖onActivityResult方法,以便当用户响应对话框时,此方法将收到结果结果。我确实在ActivityHome中覆盖了它,它满足了您的要求。我已经尝试了所有这些步骤,因为这些步骤在Google Developer页面中得到了很好的描述…但是,弹出窗口没有显示您在测试时使用的安卓操作系统版本?我已经尝试了所有这些步骤,因为这些步骤在Google Developer页面中得到了很好的描述…但是,弹出窗口不会显示您在测试时使用的android操作系统版本?