我知道有关在Android上以编程方式打开/关闭GPS的问题已经多次讨论过了。我有同样的问题,已在此链接中讨论过:
How can I enable or disable the GPS programmatically on Android?
但有没有一种方法适用于Android 6.x?
答案 0 :(得分:0)
答案 1 :(得分:0)
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 up
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();
}