android中gps的打开关闭
16lz
2022-06-14
亲测一下方法有用,4.2以后都可以用
private void toggleGPS() { Intent gpsIntent = new Intent(); gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); gpsIntent.addCategory("android.intent.category.ALTERNATIVE"); gpsIntent.setData(Uri.parse("custom:3")); try { PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send(); } catch (CanceledException e) { e.printStackTrace(); } }判断是否打开
private void isGPSEnable() { /* 用Setting.System来读取也可以,只是这是更旧的用法 String str = Settings.System.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); */ String str = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); Log.v("GPS", str); if (str != null) { return str.contains("gps"); } else{ return false; } }
更多相关文章
- android 根据SD卡中图片路径读取并显示SD中的图片――源代码
- Android(安卓)代码里设置ImageView的src和background
- Android(安卓)实现序列化:Parcelable和Serializable接口的用法
- android 使用Java自带的HttpURLConnection 连接网络 读取返回数
- Android文件保存和读取
- android 1.6全部的权限介绍
- Android中使用PULL方式解析XML文件深入介绍
- android 三级联动开源框架用法
- android 项目串口通信(serialPort)开发遇到的炕