如何在Android 6.x上以编程方式启用或禁用GPS()

智慧并不产生于学历,而是来自对于知识的终生不懈的追求。这篇文章主要讲述如何在Android 6.x上以编程方式启用或禁用GPS?相关的知识,希望能为你提供帮助。
【如何在Android 6.x上以编程方式启用或禁用GPS()】我知道有关在android上以编程方式打开/关闭GPS的问题已经多次讨论过了。我有与此链接中讨论的相同问题:
How can I enable or disable the GPS programmatically on Android?
但有没有一种适用于Android 6.x的方法?
答案首先,你需要获得gps的运行时权限,然后这将工作
请检查此链接以直接启用和禁用gps

https://stackoverflow.com/a/33555732
另一答案
private void buildAlertMessageNoGps() { final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(" Your GPS seems to be disabled, do you want to enable it?") .setCancelable(false) .setPositiveButton("?yes", new DialogInterface.OnClickListener() { public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) { startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }) .setNegativeButton("no", new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) { dialog.cancel(); } }); final AlertDialog alert = builder.create(); alert.show(); }private void turnGPSOff(){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(provider.contains("gps")){ //if gps is enabled 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); }

}
android 6中的权限下来
< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


权限android 6了
if ( ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED & & ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { } else {Toast.makeText(this, R.string.error_permission_map, Toast.LENGTH_LONG).show(); }


    推荐阅读