智慧并不产生于学历,而是来自对于知识的终生不懈的追求。这篇文章主要讲述如何在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();
}
推荐阅读
- Android GPS FusedLocationProviderClient(经度错误)
- 有没有办法清除已经从Android设备手动捕获的位置详细信息()
- 如何在Android中使用GPS服务
- 如何在android中将输入流转换为字节数组到字符串
- Android studio - 无法连接到LDAP服务器
- 用户杀死Android应用后保存状态
- Android中TextView的TextAppearance属性
- Android(为什么TextView中的文本在LinearLayout中滚动而在Recyclerview中滚动)
- Android(以编程方式添加Textview,而不是将文本包装到下一行)