如何在Android应用程序上以编程方式打开GPS
请帮助我在我的移动应用程序(如OlaCabs)中打开GPS。。 我正在使用以下代码:如何在Android应用程序上以编程方式打开GPS,android,performance,android-layout,xamarin,gps,Android,Performance,Android Layout,Xamarin,Gps,请帮助我在我的移动应用程序(如OlaCabs)中打开GPS。。 我正在使用以下代码: String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(!provider.contains("gps")){ //if gps is disabled final Intent poke = new Intent(); p
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
但它无法打开GPS。安卓指南已在4.0版以上更改。对于高于4.0的版本,您无法通过编程方式将GPS关闭为打开 不要像我那样浪费你的时间 要使用对话框,请选中它: 或者:
当手动为所有仍在寻找答案的用户启用定位时,您可以向用户显示一个对话框并将其重定向到设置: 以下是OLA出租车和其他类似应用程序的使用方法 将此添加到onCreate中
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(Login.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(Login.this, 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;
}
}
});
}
那么OlaCabs是怎么做的呢?我认为你需要安装OlaCabs并查看他的功能@Aspicas我认为你使用的是OlaCabs@Aspicas的旧版本,是的,我知道当我们打开OlaCabs应用程序时,我们会有一个对话框,要求我们启用GPS,但按下“是”按钮后GPS会自动打开。。。。请安装旧版本并查看功能。。。。在OlaCabs出现之前,我也像你一样思考,但不是现在……我建议你,首先对OlaCabs的最新移动应用程序进行概述。。实现了所需的功能@AspicasOk我检查了你的更新,它只是用来将页面重定向到设置页面,只需一行即可,即activity.StartActivity(newintent(Android.Provider.Settings.ActionLocationSourceSettings));这不是正确的解决方案@AspicasNo我需要在不重定向到应用程序设置页面的情况下打开GPS@MaxOk您找到这个问题的解决方案了吗?还没有@EmptyData,您有什么解决方案吗?这里有任何具有OLA代码访问权限的人吗P:P我想知道他们是如何在每个版本的Android上都能做到这一点的。!我认为他们要求的是不必显示对话框,这是不允许的。然而,这正是奥拉所做的!
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}