2012-01-24 79 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詢問其位置提供(多個)啓用。如果沒有啓用,您可以提示對話框更改應用程序中的設置。

相關問題