2012-01-24 81 views

回答

20

不,它不是, 但你可以打开位置服务设置窗口:

context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
-1

使用下面的代码来启动GPS

locationManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE); 
locationListener = new LocationListener(); 
locationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, 0, 0, locationListener); 

和下面的代码停止GPS

locationManager.removeUpdates(locationListener); 

详情请参阅documentations for LocationManagerfor LocationListener

+1

我正在谈论以编程方式打开android设备(设置 - >位置和安全 - >使用无线网络/使用GPS卫星)选项。 –

2
private void turnGPSOn(){ 
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); 
} 
} 

但这个代码只支持高达8 APK

1

是的,但你必须是一个超级用户(须藤),您的设备必须植根。

6

与模式的高精确度或节约耗电量,而无需用户需要访问设置

http://android-developers.blogspot.in/2015/03/google-play-services-70-places-everyone.html

+1

从Google Play Services 7.0开始,这应该是被接受的答案。您可以在不离开应用的情况下进行应用更新设置。参见上面的链接。 –

+2

这个链接并没有说太多的实现,我错过了什么? –

+0

执行示例在这里:http://stackoverflow.com/questions/33251373/turn-on-location-services-without-navigating-to-settings-page – MorZa

0

随着最近棉花糖更新,即使位置设置打开启用位置,您的应用程序将需要明确要求允许。推荐的方式是显示应用的权限部分,其中用户可以根据需要切换权限。这样做的代码片段如下:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  
    if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  
     final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Location Permission"); 
     builder.setMessage("The app needs location permissions. Please grant this permission to continue using the features of the app."); 
     builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
  
      } 
     }); 
     builder.setNegativeButton(android.R.string.no, null); 
     builder.show(); 
    } 
} else { 
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    boolean isGpsProviderEnabled, isNetworkProviderEnabled; 
    isGpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    isNetworkProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

    if(!isGpsProviderEnabled && !isNetworkProviderEnabled) { 
     final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Location Permission"); 
     builder.setMessage("The app needs location permissions. Please grant this permission to continue using the features of the app."); 
     builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       startActivity(intent); 
      } 
     }); 
     builder.setNegativeButton(android.R.string.no, null); 
     builder.show(); 
    } 
} 

并重写为下面的onRequestPermissionsResult方法:

@Override 
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case PERMISSION_REQUEST_COARSE_LOCATION: { 
      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       Log.d(TAG, "coarse location permission granted"); 
      } else { 
       Intent intent = new Intent(); 
       intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
       Uri uri = Uri.fromParts("package", getPackageName(), null); 
       intent.setData(uri); 
       startActivity(intent); 
      } 
     } 
    } 
} 

另一种方法是,你还可以使用SettingsApi询问其位置提供(多个)启用。如果没有启用,您可以提示对话框更改应用程序中的设置。

相关问题